While Loop not working

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?

Notice: I’m making an AI here (FNaF so)

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.

Doesn’t work. Already tried that, which is why I also came here because I know something is seriously wrong.

There are no errors in console? I see an unnecessary end in your while loop (maybe due to the forum post, not sure if it’s there in your game).

1 Like

Accidentally put it there, don’t worry about it
There’s no errors.

Have you added a print inside the while loop to see if it is running? Perhaps it is just the function that is not working. Just a thought.

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

in this function

requestMove.OnServerEvent:Connect(function(Player,Animatronic)
	turnedOn = true
end)
1 Like

Is the requestMove remote even fired?

I did some testing and discovered something.

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.

2 Likes

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)

I wanted the AI to be activated at a certain time ingame, that’s why the remote event exists
The loop code contains all the AI after.