I am trying to make NPC that is randomly walking and attacks players - roblox

I am trying to make NPC that is randomly walking and attacks players. Can someone help me?

A good starting place for NPCs that walk around and attack players are zombies. I would recommend looking at the R15 Zombie with #### sounds model in the Toolbox. Once you drag it into the Workspace, there is a script in there called Script that is fairly straightforward in how it moves the zombie around to attack other humanoid characters.

Well, that is complex in itself, but if you are new to scripting, it might be best to just get the free-modeled zombie in the toolbox, and then change it's appearance through the explorer window. If you want to go the long route, it might be better to learn how to use the function :MoveTo. The Roblox Developer website provides good info on how to use these things. As for attacking people, just put a script in the arm that damages on touch. Here's something that might work, (put it in the same model as the humanoid):
local larm = script.Parent:FindFirstChild("Left Arm")
local rarm = script.Parent:FindFirstChild("Right Arm")
function findNearestTorso(pos)
local list = game.Players:GetPlayers()
local torso = nil
local dist = 10
local temp = nil
local human = nil
local temp2 = nil
for x = 1, #list do
temp2 = list[x]
if temp2.Character:IsA("Model") then
temp = temp2.Character:FindFirstChild("Torso")
human = temp2.Character:FindFirstChild("Humanoid")
if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
if (temp.Position - pos).magnitude < dist then
torso = temp
dist = (temp.Position - pos).magnitude
end
end
end
end
return torso
end
while true do
wait(0.1)
local target = findNearestTorso(script.Parent.Torso.Position)
if target ~= nil then
script.Parent.Humanoid:MoveTo(target.Position, target)
end
end
For the damage script, you can use this if you add it to one of the arms:
script.Parent.Touched:connect(function(part)
if part.Parent:FindFirstChild("Humanoid") then
part.Parent.Humanoid:TakeDamage(30)
end
end)

Related

Roblox Pathfinding Back and forth movement when reaching the destination requires jumping

For some reason when the destination target is placed somewhere where it requires jumping to get to my NPC is moving towards it while keep switching between moving back and forth.
Code:
local path = PathfindingService:CreatePath()
local waypoints
local currentWaypointIndex
local function followPath(destinationObject)
path:ComputeAsync(enemyNPC.HumanoidRootPart.Position, destinationObject.Position)
waypoints = {}
if path.Status == Enum.PathStatus.Success then
waypoints = path:GetWaypoints()
currentWaypointIndex = 1
humanoid:MoveTo(waypoints[currentWaypointIndex].Position)
else
error("Path not really working")
end
end
local function onWaypointReached(reached)
if reached and currentWaypointIndex < #waypoints then
currentWaypointIndex += 1
if waypoints[currentWaypointIndex].Action == Enum.PathWaypointAction.Jump then
humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
humanoid:MoveTo(waypoints[currentWaypointIndex].Position)
end
if reached and currentWaypointIndex >= #waypoints then
print("Target reached!")
animationTrack:Stop()
end
end
local function onPathBlocked(blockWaypointIndex)
if blockWaypointIndex > currentWaypointIndex then
followPath(destination)
end
end
path.Blocked:Connect(onPathBlocked)
humanoid.MoveToFinished:Connect(onWaypointReached)
followPath(destination)
(I delted the top part of the code with some local variables because it didn't let me post the whole thing without saying my post is mostly code)
Does anybody know what am I doing wrong?
You're looking for the action in the PathWaypoint, which is an PathWaypointAction Enum. You have to determine whether the PathWaypoint is a Walk or Jump action. Currently, you're doing MoveTo() regardless of whether it has to jump or not.
So, in your code:
if waypoints[currentWaypointIndex].Action == Enum.PathWaypointAction.Jump then
humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
else
​humanoid:MoveTo(waypoints[currentWaypointIndex].Position)
end

How do you clone a MODEL on Roblox?

I have seen videos and stuff on how to clone things in Roblox, but most of it is just "How to clone parts in Roblox!" and "How to clone OBJECTS in Roblox!" But still is only showing parts. My goal here is to make a system that when you touch a part, your character gets cloned to a position in the workspace, while a cut scene plays. This is like the system used in Mini Toon's game "Piggy" that broke records. I do not need help with any of the cut scene things though, I got that covered.
wait(10)
copy = game.Workspace.YourUsernameHere:clone()
copy.Parent = game.Workspace
copy.Name = "Test"
copy.Posistion = Vector3.new(0, 0, 0)
This is one of the things I have seen. I hope someone can help me with this.
The Archivable property must be set to true in order to clone the player's Character.
wait(10)
game.Workspace.YourUsernameHere.Archivable = true
copy = game.Workspace.YourUsernameHere:clone()
game.Workspace.YourUsernameHere.Archivable = false
copy.Parent = game.Workspace
copy.Name = "Test"
copy:MoveTo(Vector3.new(0, 0, 0))
Use Model:SetPrimaryPartCFrame()

Roblox; making destroyed parts reappearing

I've made a game where you step on a specific tile this part gets destroyed. I want to make it that after a certain amount of time this destroyed block will reappear, now you might wonder why I just don't make the part invisible and make it lose it's player collision. I have not done this because I don't know how to make the Texture on top of the part transparency 1.
You could make a copy of the part, then do the destruction, and put the copy back into its place after a few seconds:
function onTouched(hit)
-- see if it was a player that touched the part
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if (plr == nil) then return end
-- make backup of the part and its parent
local part = workspace.Part
local backup = part:Clone()
local backupParent = part.Parent
part:Destroy() -- do some cool effect for the destruction of it...
spawn(function()
wait(5)
-- put part back in place
backup.Parent = backupParent
backup.Touched:Connect(onTouched)
end)
end
workspace.Part.Touched:Connect(onTouched)
If you just want it to disappear you could also just remove it from the object tree temporarily by setting its Parent to nil:
function onTouched(hit)
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if (plr == nil) then return end
local part = workspace.Part
local backupParent = part.Parent
part.Parent = nil
spawn(function()
wait(5)
part.Parent = backupParent
end)
end

Damage player when touching water terrain

I'm trying to decrease a player's health when they touch some water terrain in ROBLOX.
I'm not sure why this doesn't work, but it doesn't. If someone could help me out that would be neat.
while wait() do
local player = game.Workspace.LocalPlayer
local headLoc = game.Workspace.Terrain:WorldToCell(player.Character.Head.Position)
local hasAnyWater = game.Workspace.Terrain:GetWaterCell(headLoc.x, headLoc.y, headLoc.z)
if player.Character.Humanoid.Health ~= 0 then
if hasAnyWater then
player.Character.Humanoid:TakeDamage(0.2)
end
end
end
If you check your output window you'll see why:
LocalPlayer is not a valid member of Workspace
LocalPlayer is in "Players", so you should declare:
local player = game.Players.LocalPlayer
So if you change that, and have it in a LocalScript for example in the StarterPlayerScripts folder, everything works just like you want.
Solved.
local player = game:GetService("Players").LocalPlayer
while wait(0.5) do
local headLoc = game.Workspace.Terrain:WorldToCell(player.Character.LowerTorso.Position) or game.Workspace.Terrain:WorldToCell(player.Character.Torso.Position)
local hasAnyWater = game.Workspace.Terrain:GetWaterCell(headLoc.x, headLoc.y, headLoc.z)
if player.Character.Humanoid.Health ~= 0 then
if hasAnyWater then
player.Character.Humanoid:TakeDamage(8)
end
end
end

How can I make a Humanoid killable, and have it respawn? (Roblox)

Title. All tutorials I have found only explain how to make a human model, not how to make it killable. i have set the humanoid's Health and Max health to 100, and it's showing the health bar, but it cannot take damage.
Thanks!
It depends where the humanoid is taking damage from. If it is taking damage from a LocalScript that does not support FE, then, the Humanoid would not take damage.
If you want to make a Humanoid take damage, or kill it instantly, here are some methods:
Killing instantly (from a non - LocalScript)
local character = nil; -- replace nil with the character
character:BreakJoints();
Killing instantly (from a LocalScript)
In a local script:
local character = game:service("Players").LocalPlayer.Character -- replace this to the character. if you don't replace it, it will kill your character.
local rep = game:service("ReplicatedStorage");
local event = rep:WaitForChild("killEvent");
event:FireServer(character);
In a normal script:
local rep = game:service("ReplicatedStorage");
local ev = Instance.new("RemoteEvent",rep);
ev.Name = "killEvent";
ev.OnServerEvent:connect(function(plr, char)
if not char then char = plr.Character end;
char:BreakJoints();
end);
Taking a amount of damage (from a non - LocalScript)
local character = nil; -- replace nil with the character
local humanoid = character:WaitForChild("Humanoid");
local damage = 50; -- Change this to the damage you want the humanoid to take
humanoid:TakeDamage(damage);
Taking a amount of damage (from a LocalScript)
In a local script:
local rep = game:service("ReplicatedStorage");
local event = rep:WaitForChild("damageEvent");
local character = game:service("Players").LocalPlayer.Character; -- replace this to the character. if you don't change it, it will automatically damage your character.
local humanoid = character:WaitForChild("Humanoid");
local damage = 50; -- Change this to the damage you want the player to take
event:FireServer(humanoid, damage);
In a normal script:
local rep = game:service("ReplicatedStorage");
local event = Instance.new("RemoteEvent", rep);
event.Name = "damageEvent";
event.OnServerEvent:connect(function(player, hum, damage)
if not hum then hum = player.Character.Humanoid; end;
hum:TakeDamage(damage);
end);
That should do it for making a Humanoid take damage or to kill it. You can also use these methods to kill other players, since we use Remotes for locals.
Hope my answer helps!