I’ve been here for quite long now… (3 hours), the reason behind it, is because I’ve been trying to figure out this issue. I gave up just now, and I’ll make this forum for help.
local replicatedStorage = game:GetService("ReplicatedStorage")
local events = replicatedStorage:FindFirstChild("RemoteEvents")
local jpEvent = events["Jumpscare"]
local requestMove = events["TYRequestMove"]
local requestMoveClient = events["RequestMove"]
local checkersModel = workspace:FindFirstChild("ToyCheckers")
local turnedOn = false
local waitTimes = {"3", "2", "5", "15", "6", "9", "21"}
requestMove.OnServerEvent:Connect(function(Player,Animatronic)
turnedOn = true
end)
function moveCheckers(pos)
checkersModel.HumanoidRootPart.CFrame = Vector3.new(pos)
requestMoveClient:FireAllClients("ToyCheckers")
end
while turnedOn == true do
local waitTimeResult = waitTimes[math.random(1,#waitTimes)]
wait(waitTimeResult)
moveCheckers(86.529, 4.038, -58.56)
wait(1000)
end
end
Basically, the “while turnedOn == true” isn’t working when it’s set as you can see inside the RemoteEvent. Does anyone know why this happens?
Just to test, can you add a print function (of anything) inside the RemoteEvent listener. This is to make sure the function is actually being triggered.
i could be wrong, maybe the while loop only runs once in the script. That being it never runs “while” when you fire the server
try putting
while turnedOn == true do
local waitTimeResult = waitTimes[math.random(1,#waitTimes)]
wait(waitTimeResult)
moveCheckers(86.529, 4.038, -58.56)
wait(1000)
end
end
The while loop will not begin if the conditions are not met (obviously). The loop does NOT detect when the values stated in the loop conditions are changed to true, ONLY when it is originally true, then changes to false. Not vice-versa. You need to move the loop into the OnServerEvent listener.
This is the right answer - the loop runs at the start, sees that turnedOn is false since the remote hasn’t gotten a chance to fire yet, and ends immediately.
I think the behaviour you actually wanted was for the moveCheckers function to be called everytime the remote is fired after a slight wait. In this case you can do away with the loop and just put the code within the .OnServerEvent connection.
requestMove.OnServerEvent:Connect(function(Player,Animatronic)
local waitTimeResult = waitTimes[math.random(1,#waitTimes)]
task.wait(waitTimeResult) -- Consider using task.wait instead of wait: https://devforum.roblox.com/t/1387845
moveCheckers(86.529, 4.038, -58.56)
end)