How do I get my while true loop to work after the enabled variable turns true?
local Brick = script.Parent
local PP = script.Parent.ProximityPrompt
local Rewards = require(game.ReplicatedStorage.Rewards)
local Enabled = false
local Status = "Off"
if Enabled then
while true do -- when enabled turns true, I want this loop to run, but it doesn't work.
wait(3)
for _, plr in pairs(game.Players:GetChildren()) do
local cash = plr:FindFirstChild("leaderstats")
if cash then
cash.Cash.Value = cash.Cash.Value + Rewards.FreeMoney
end
end
end
else
print("not enabled, status: "..Status)
end
PP.Triggered:Connect(function()
Enabled = true
local Status = "On"
print("enabled, status: "..Status)
end)
This doesnât seem to work, I dunno if I did it wrong or what.
What happens is the output is flooded w/ the print statement in âelseâ, and if I remove the Enabled2 variable and make the loop that surrounds the if statement, the proximity prompt doesnât work
local Brick = script.Parent
local PP = script.Parent.ProximityPrompt
local Rewards = require(game.ReplicatedStorage.Rewards)
local Enabled = false
local Enabled2 = false
local Status = "Off"
while Enabled == Enabled2 do
if Enabled then
while true do
wait(3)
for _, plr in pairs(game.Players:GetChildren()) do
local cash = plr:FindFirstChild("leaderstats")
if cash then
cash.Cash.Value = cash.Cash.Value + Rewards.FreeMoney
end
end
end
else
print("not enabled, status: "..Status)
end
task.wait(1)
end
PP.Triggered:Connect(function()
Enabled = true
Enabled2 = true
local Status = "On"
print("enabled, status: "..Status)
end)
Instead of a bool variable, create a boolValue somewhere, and then you can just use .Changed to run code when it changes to true. Kinda simple but it should work
local PP = script.Parent.ProximityPrompt
local Rewards = require(game.ReplicatedStorage.Rewards)
local Enabled = script.Parent.Enabled
local Status = "Off"
while Enabled.Value == true do
wait(3)
if Enabled then
while true do
wait(3)
for _, plr in pairs(game.Players:GetChildren()) do
local cash = plr:FindFirstChild("leaderstats")
if cash then
cash.Cash.Value = cash.Cash.Value + Rewards.FreeMoney
print("collecting money!")
end
end
end
else
print("not enabled, status: "..Status)
end
end
PP.Triggered:Connect(function()
Enabled.Value = true
local Status = "On"
print("enabled, status: "..Status)
end)
I was about to go to bed, but then the solution came to my mind. I needed to put the if statement in a function.
local Brick = script.Parent
local PP = script.Parent.ProximityPrompt
local Rewards = require(game.ReplicatedStorage.Rewards)
local Enabled = script.Parent.Enabled
local Status = "Off"
local function EarnCash()
if Enabled then
while true do
wait(3)
for _, plr in pairs(game.Players:GetChildren()) do
local cash = plr:FindFirstChild("leaderstats")
if cash then
cash.Cash.Value = cash.Cash.Value + Rewards.FreeMoney
print("collecting money!")
end
end
end
else
print("not enabled, status: "..Status)
end
end
PP.Triggered:Connect(function()
Enabled.Value = true
local Status = "On"
print("enabled, status: "..Status)
EarnCash()
end)