I wrote this script meant to move an NPC 10-30 seconds after a light goes out but he never moves. Can somebody tell me what’s wrong and how to fix it?
Door = game.Workspace.DoorPos
DoorFront = game.Workspace.DoorFront
humanoid = script.Parent
Smile = script.Parent.Parent.Head.faces.Smile
Regular = script.Parent.Parent.Head.faces.Regular
if game.Workspace.Lights.Main.One.Light.SpotLight.Enabled == false then
wait(math.random(10,20))
if game.Workspace.Lights.Main.One.Light.SpotLight.Enabled == false then
script.Parent.Parent = game.Workspace
humanoid:MoveTo(Door.Position)
wait(0.2)
humanoid:MoveTo(DoorFront.Position)
game.Workspace.SecDoor.Unionz.Sound2:Play()
end
end
I think the problem you have is that the code above will only ever run once (when the script is parented to the NPC), and it will never run again. And so, the lights are probably on when the code runs so the NPC never moves.
The solution to this is to make sure that this bit of code runs whenever the lights change. This can be done using the PropertyChangedSignal event.
e.g.
function checkLightsOut()
-- put your logic for moving the NPC in here.
end
-- Connect an event that will call the checkLightsOut function whenever the
-- 'Enabled' property on the spotlight changes.
spotlightInstance:GetPropertyChangedSignal("Enabled"):Connect(checkLightsOut)