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
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
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.
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?
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