While loop only runs once?

So i have this while loop which is supposed to give money every 2 seconds, but it only runs once and stops. I had prints to check this and it only printed once.

This is in a Server Script

while wait(2) do
	if sitting then
	local money = math.floor(math.random(price.Value/formula1) + price.Value/formula2)
	local name = player.Parent.Name
	local gear = car["A-Chassis Tune"]["A-Chassis Interface"].Drive.getGear:InvokeClient(game.Players[name])
	if gear ~= 0 then
	game.Players[name].leaderstats.Money.Value = game.Players[name].leaderstats.Money.Value + money
	end
	end
end
1 Like

Try this instead, basically if you change the While loop to this it will go on forever, but make sure you have a wait() in it so it won’t crash.

Here is the code:

while true do
	if sitting then
	local money = math.floor(math.random(price.Value/formula1) + price.Value/formula2)
	local name = player.Parent.Name
	local gear = car["A-Chassis Tune"]["A-Chassis Interface"].Drive.getGear:InvokeClient(game.Players[name])
	if gear ~= 0 then
	game.Players[name].leaderstats.Money.Value = game.Players[name].leaderstats.Money.Value + money
	end
	end
    wait(2) 
end

I think that the cause might be sitting being false. Use a print() in the loop to check the value of sitting.

I’ve also tried that but it brings the same result.

Ok i’ll try doing that right now.

Ok it’s not that either, when i print it above everything it just prints true in the console

That is not true. You can use wait as the condition and it will wait X before running the loop every time.

Also, wait() will not crash it. Having while true do with no wait at all is what will crash it.

3 Likes

Ok so this is my whole script

local car = script.Parent

local player
local price = car.DriveSeat:WaitForChild("Price")

local moneyDelay = 2
local formula1,formula2 = 100,500

driveState = false
sitting = false

car.DriveSeat.Changed:Connect(function()
	if car.DriveSeat.Occupant ~= nil then
		player = car.DriveSeat.Occupant
		sitting=true
		print("player sat in seat "..player.Parent.Name)
	else
		if sitting then
		print("player left seat")
		sitting=false
		player = nil
		end
	end
end)

while wait(2) do
	print(sitting)
	if sitting then
	local money = math.floor(math.random(price.Value/formula1) + price.Value/formula2)
	local name = player.Parent.Name
	local gear = car["A-Chassis Tune"]["A-Chassis Interface"].Drive.getGear:InvokeClient(game.Players[name])
	if gear ~= 0 then
	game.Players[name].leaderstats.Money.Value = game.Players[name].leaderstats.Money.Value + money
	end
	end
end

It only prints ‘true’ once which means it’s meant to work im pretty sure.

1 Like

Did you put it inside the loop?

1 Like

I said make sure you have a wait() in it so it won’t crash.

But also, I didn’t know you could make wait the condition too.

1 Like

Ah, i think i’ve found out what’s wrong, it waits until the remote function returns something and it doesn’t continue. However, it should still continue something since it returns a value.

This is my code for the remote function

	script.getGear.OnClientInvoke = function(plr)
		print("Recieved "..plr.Name)
		return _CGear
	end

Ok, i just noticed, when it’s ran it doesn’t print anything inside the invoke function, should i not use a remote function?

2 Likes

Hi, I’m having a problem similar to this one

I have a while loop checking every 100th of a sec if a potion effect is true or false, and making an image visible or not accordingly, but it will run and make the image show, but when the effect goes false, the next time it is true the image doesn’t reappear

while true do
wait(0.01)
local effect = game.Players.LocalPlayer:WaitForChild(“Stuff”):WaitForChild(“Potions”):WaitForChild(“Effects”):WaitForChild(“Fortune Potion”)
if effect.Value == true then
script.Parent.Visible = true
elseif effect.Value == false then
script.Parent.Visible = false
end
end