I am trying to add a script that changes the animation of an npc depending on what time of day it is. So if it is day time, the npc stands up and when it is night time, the npc sits down.
But the sitdowntrack and getuptrack keep playing after activated. They are not looped animations so I’m not sure what is the issue and there is no error in the output
local humanoid = script.Parent:WaitForChild("Humanoid")
local idle = script.Idle
local sitdown = script.SitDown
local sit = script.Sit
local getup = script.GetUp
local idletrack = humanoid:LoadAnimation(idle)
local sitdowntrack = humanoid:LoadAnimation(sitdown)
local sittrack = humanoid:LoadAnimation(sit)
local getuptrack = humanoid:LoadAnimation(getup)
while true do
local Time = game.Lighting.TimeOfDay
wait(0.1)
Time = (Time:gsub("[:\r]", ""))
Time = tonumber(Time)
if Time >= 180000 or Time < 60500 then
wait(4)
idletrack:Stop()
sitdowntrack:Play()
wait(4)
sitdowntrack:Stop()
sittrack:Play()
else
wait(4)
sittrack:Stop()
getuptrack:Play()
wait(4)
getuptrack:Stop()
idletrack:Play()
end
end
Well it looks like you put them in a while true do loop, which will cause the animations to repeatedly loop.
Instead of using a while loop, could you use the GetPropertyChangedSignal event on the lighting’s TimeOfDay value, and based on the time of day, play the certain animation?
I’m a bit new to scripting so I don’t really understand how to do that. I made a seperate script but all that does is play them once rather then when the time changes.
local humanoid = script.Parent:WaitForChild("Humanoid")
local sitdown = script.SitDown
local getup = script.GetUp
local sitdowntrack = humanoid:LoadAnimation(sitdown)
local getuptrack = humanoid:LoadAnimation(getup)
local Time = game.Lighting.TimeOfDay
wait(0.1)
Time = (Time:gsub("[:\r]", ""))
Time = tonumber(Time)
if Time >= 180000 or Time < 60500 then
wait(0.1)
sitdowntrack:Play()
else
wait(0.1)
getuptrack:Play()
end
local humanoid = script.Parent:WaitForChild("Humanoid")
local sitdown = script.SitDown
local getup = script.GetUp
local sitdowntrack = humanoid:LoadAnimation(sitdown)
local getuptrack = humanoid:LoadAnimation(getup)
game:GetService("Lighting"):GetPropertyChangedSignal("TimeOfDay"):Connect(function()
local Time = game.Lighting.TimeOfDay
print(Time)
wait(0.1)
Time = (Time:gsub("[:\r]", ""))
Time = tonumber(Time)
if Time >= 180000 or Time < 60500 then
wait(0.1)
sitdowntrack:Play()
else
wait(0.1)
getuptrack:Play()
end
end)
See if this works. Basically what you’re doing here is using the GetPropertyChangedSignal on the “TimeOfDay” property of the service “Lighting”, so whenever you change the time of day, this function will get fired.
I can’t see that video format on my phone, and I’m unable to access my computer at the moment.
I have a feeling this is due to your TimeOfDay changing continually, which means the function is run multiple times which causes the animation to play multiple times. Instead of checking if the value is > another value, maybe check to see if it’s == to a certain TimeOfDay, so the animation will only play when that TimeOfDay value is reached.