Hi, never posted on here, but a lot of the posts are helpful!!! Ok anyways, im working on this npc/rig/thing which like moves in random directions. I need it to play the walk animation when it’s moving and play the idle animation when it’s not. I’ve tried a bunch of different ways and looked at a couple posts on here but couldn’t find anything that worked. here’s my script btw:
while true do
local randx = math.random(-50,50)
local randz = math.random(-50,50)
local direction = math.random(1,2)
local timer = 10
if direction == 1 then
local newx = ghost.HumanoidRootPart.Position.X + randx
local newposx = Vector3.new(newx, ghost.HumanoidRootPart.Position.Y, ghost.HumanoidRootPart.Position.Z)
ghost.Humanoid:MoveTo(newposx)
idleanim:Stop()
walkanim:Play()
repeat
timer -= 1
wait(1)
until ghostpart.Position == newposx or timer == 0
walkanim:Stop()
idleanim:Play()
else
local newz = ghost.HumanoidRootPart.Position.Z + randz
local newposz = Vector3.new(ghost.HumanoidRootPart.Position.X, ghost.HumanoidRootPart.Position.Y, newz)
ghost.Humanoid:MoveTo(newposz)
idleanim:Stop()
walkanim:Play()
repeat
timer -= 1
wait(1)
until ghostpart.Position == newposz or timer == 0
walkanim:Stop()
idleanim:Play()
end
wait()
end
to be clear, the movement works but the animation will not play for some reason. i’ve actually been doing this for like days please help!
-- variables
local ghost = script.Parent
local ghosthuman = ghost.Humanoid
local ghostpart = ghost.HumanoidRootPart
local field = ghost.field
local chaseSpeed = game.Workspace.Values.chasespeed
local walkanim = ghosthuman:LoadAnimation(ghost.Animate.walk.WalkAnim)
local idleanim = ghosthuman:LoadAnimation(ghost.Animate.idle.Animation1)
-- entering ghost proximity, chasing
field.Touched:Connect(function(touchedPart)
if touchedPart.Parent:FindFirstChild("Humanoid") then
local character = touchedPart.Parent
local humanpart = character:WaitForChild("HumanoidRootPart")
print("Ghost touched " .. character.Name)
repeat
ghosthuman.WalkSpeed = chaseSpeed.Value
ghosthuman:MoveTo(humanpart.Position)
wait(5)
until
ghostpart.Position == humanpart.Position or field.TouchEnded
wait(0)
end
end)
-- wandering
while true do
local randx = math.random(-50,50)
local randz = math.random(-50,50)
local direction = math.random(1,2)
local timer = 10
if direction == 1 then
local newx = ghost.HumanoidRootPart.Position.X + randx
local newposx = Vector3.new(newx, ghost.HumanoidRootPart.Position.Y, ghost.HumanoidRootPart.Position.Z)
ghost.Humanoid:MoveTo(newposx)
idleanim:Stop()
walkanim:Play()
repeat
timer -= 1
wait(1)
until ghostpart.Position == newposx or timer == 0
walkanim:Stop()
idleanim:Play()
else
local newz = ghost.HumanoidRootPart.Position.Z + randz
local newposz = Vector3.new(ghost.HumanoidRootPart.Position.X, ghost.HumanoidRootPart.Position.Y, newz)
ghost.Humanoid:MoveTo(newposz)
idleanim:Stop()
walkanim:Play()
repeat
timer -= 1
wait(1)
until ghostpart.Position == newposz or timer == 0
walkanim:Stop()
idleanim:Play()
end
wait()
end
thank you guys for all the replies, but i figured it out on my own! here’s my script:
-- variables
local ghost = script.Parent
local ghosthuman = ghost.Humanoid
local ghostpart = ghost.HumanoidRootPart
local field = ghost.field
local chaseSpeed = game.Workspace.Values.chasespeed
local walkanim = ghosthuman:LoadAnimation(ghost.Animate.walk.WalkAnim)
local idleanim = ghosthuman:LoadAnimation(ghost.Animate.idle.Animation1)
-- entering ghost proximity, chasing
field.Touched:Connect(function(touchedPart)
if touchedPart.Parent:FindFirstChild("Humanoid") then
local character = touchedPart.Parent
local humanpart = character:WaitForChild("HumanoidRootPart")
print("Ghost touched " .. character.Name)
repeat
ghosthuman.WalkSpeed = chaseSpeed.Value
ghosthuman:MoveTo(humanpart.Position)
wait(5)
until
ghostpart.Position == humanpart.Position or field.TouchEnded
wait(0)
end
end)
-- wandering
while true do
local randx = math.random(-50,50)
local randz = math.random(-50,50)
local direction = math.random(1,2)
local timer = 10
if direction == 1 then
local newx = ghost.HumanoidRootPart.Position.X + randx
local newposx = Vector3.new(newx, ghost.HumanoidRootPart.Position.Y, ghost.HumanoidRootPart.Position.Z)
ghosthuman:MoveTo(newposx)
print("walk")
idleanim:Stop()
walkanim:Play()
repeat
timer -= 1
wait(1)
until ghosthuman.MoveToFinished
timer = 10
print("pause")
walkanim:Stop()
idleanim:Play()
else
local newz = ghost.HumanoidRootPart.Position.Z + randz
local newposz = Vector3.new(ghost.HumanoidRootPart.Position.X, ghost.HumanoidRootPart.Position.Y, newz)
ghosthuman:MoveTo(newposz)
print("walk")
idleanim:Stop()
walkanim:Play()
repeat
timer -= 1
wait(1)
until ghosthuman.MoveToFinished
timer = 10
print("pause")
walkanim:Stop()
idleanim:Play()
end
wait(math.random(1, 10))
end