Animation Delay

function WaiterWalking()
	
	while NPCSCanWalkValue.Value == true do
		
		if IsLightValue.Value == true then
			NightAnim:Stop()
			DayAnim:Play()
			Humanoid.WalkSpeed = 12
		end
		if IsLightValue.Value == false then
			DayAnim:Stop()
			NightAnim:Play()
			Humanoid.WalkSpeed = 15
		end
		
		local randomPart = math.random(1, 8)
		local randomWait = math.random(0.2, 1.5)
		
		wait(randomWait)

		if randomPart == 1 then
			Humanoid:MoveTo(Part1.Position)
			Humanoid.MoveToFinished:Wait()
		end
		if randomPart == 2 then
			Humanoid:MoveTo(Part2.Position)
			Humanoid.MoveToFinished:Wait()
		end
		if randomPart == 3 then
			Humanoid:MoveTo(Part3.Position)
			Humanoid.MoveToFinished:Wait()
		end
		if randomPart == 4 then
			Humanoid:MoveTo(Part4.Position)
			Humanoid.MoveToFinished:Wait()
		end
		if randomPart == 5 then
			Humanoid:MoveTo(Part5.Position)
			Humanoid.MoveToFinished:Wait()
		end
		if randomPart == 6 then
			Humanoid:MoveTo(Part6.Position)
			Humanoid.MoveToFinished:Wait()
		end
		if randomPart == 7 then
			Humanoid:MoveTo(Part7.Position)
			Humanoid.MoveToFinished:Wait()
		end
	end
	
end

StartNPCSEvent.Event:Connect(WaiterWalking)

I want DayAnim & NightAnim to play right when IsLight Value is set to true or false.
I know why It doesn’t play right away, and it’s because of the Humanoid.MoveToFinished:Wait(), but I don’t know how else to do it.

If you need more information, just ask!
Any help is appreciated!

1 Like

This code looks horrendous my guy. Let me fix it a bit:

local PARTS = path.to.partsFolder:GetChildren()

function WaiterWalking()
	IsLightValue.Changed:Connect(function()
        if IsLightValue.Value == true then
			NightAnim:Stop()
			DayAnim:Play()
			Humanoid.WalkSpeed = 12
		end
		if IsLightValue.Value == false then
			DayAnim:Stop()
			NightAnim:Play()
			Humanoid.WalkSpeed = 15
		end
    end)


	while NPCSCanWalkValue.Value == true do
		local randomPart = PARTS[math.random(1, #PARTS)]
		local randomWait = math.random(0.2, 1.5)
		
		wait(randomWait)

		Humanoid:MoveTo(SelectedPart.Position)
		Humanoid.MoveToFinished:Wait()
	end
	
end

StartNPCSEvent.Event:Connect(WaiterWalking)
2 Likes

Wow! You solved the issue, and made my code like 100x better.
The IsLight.Changed function was the solution that whole time!

Whenever I have to list a bunch of parts I always forget the GetChildren() function.

Thank you!

local PurpleShampoo = {
	[1] = function(Humanoid)
		Humanoid:MoveTo(Part1.Position)
		Humanoid.MoveToFinished:Wait()
	end,
	[2] = function(Humanoid)
		Humanoid:MoveTo(Part2.Position)
		Humanoid.MoveToFinished:Wait()
	end,
	[3] = function(Humanoid)
		Humanoid:MoveTo(Part3.Position)
		Humanoid.MoveToFinished:Wait()
	end,
	[4] = function(Humanoid)
		Humanoid:MoveTo(Part4.Position)
		Humanoid.MoveToFinished:Wait()
	end,
	[5] = function(Humanoid)
		Humanoid:MoveTo(Part5.Position)
		Humanoid.MoveToFinished:Wait()
	end,
	[6] = function(Humanoid)
		Humanoid:MoveTo(Part6.Position)
		Humanoid.MoveToFinished:Wait()
	end,
	[7] = function(Humanoid)
		Humanoid:MoveTo(Part7.Position)
		Humanoid.MoveToFinished:Wait()
	end,
}

function WaiterWalking()

	while NPCSCanWalkValue.Value == true do

		if IsLightValue.Value == true then
			NightAnim:Stop()
			DayAnim:Play()
			Humanoid.WalkSpeed = 12
		end
		if IsLightValue.Value == false then
			DayAnim:Stop()
			NightAnim:Play()
			Humanoid.WalkSpeed = 15
		end

		local randomPart = math.random(1, 8)
		local randomWait = 1 -- MATH RANDOM DOES NOT RETURN DECIMALS!
                --        Humanoid:MoveTo(Part:FindFirstChild(randomPart).Position)

		task.wait(randomWait) 
        PurpleShampoo[randomPart](Humanoid)
		
	end

end

StartNPCSEvent.Event:Connect(WaiterWalking)

1 Like

Oh I didn’t know math.random didn’t return decimals?!?!

I just put this instead:

local randomWait = math.random(20, 150) / 100
print(randomWait)

Now it prints out decimals.
Thank you, I didn’t notice that.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.