How do I get my if statement to run again?

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)

I think you have to make some kind of loop before because the ‘if’ statement only checks if its true once, unless its inside another loop.

1 Like

Put it in a

while value == value2 do
--code
task.wait() --You can add a second or something if you want
end

or you can put a repeat loop until the value is what you want. then put the code under it.

1 Like

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 :confused:

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)

Yeah I accidently put the Comment to put your code inside the loop. You leave a wait in there then put it outside, There may be a better way, tho.

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

1 Like

now it just runs automatically, whether the boolvalue is on or not.

what does the code look like now? maybe you forgot to change your if statement to “if Enabled.Value == true then”

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)