Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

My while loop has a problem with a variable?

I have a variable in a folder called VAR in the workspace, which DEFINITELY becomes true after the end of a script, because I got it to print it. However, this while loop isn't working. It's supposed to be cloning a model in ServerStorage into different positions, but the script doesn't run at all. Please help! Thank you.

while game.Workspace.VAR.timeformon.Value==true do
	local modal = game.ServerStorage.IODOLLARS:Clone()
	modal.Parent=game.Workspace
	modal.Position=Vector3.new(98.05, 4.05, 228.65)
	local modol = game.ServerStorage.IODOLLARS:Clone()
	modol.Parent=game.Workspace
	modol.Position=Vector3.new(76.75, 2.85, 219.85)
	local modl = game.ServerStorage.IODOLLARS:Clone()
	modl.Parent=game.Workspace
	modl.Position=Vector3.new(89.45, 6.95, 191.55)
	local modlo = game.ServerStorage.IODOLLARS:Clone()
	modlo.Parent=game.Workspace
	modlo.Position=Vector3.new(82.05, 18.05, 228.55)
	game.Workspace.VAR.timeformon.Value=false

end

2 comments

0

Is it a model or is it a part that your cloning?

1 answer

✓ Accepted answer

I think what's happening here is that the script is going past the while loop before the value is true, so it never runs that code. Since the script cant move back up when you change it to true, you need to use something like an event. Theres an event called :GetPropertyChangedSignal() that should do you well.

local timeformon = game.Workspace.VAR.timeformon
timeformon:GetPropertyChangedSignal("Value"):Connect(function()
	while game.Workspace.VAR.timeformon.Value == true do
		wait()
		--put all that code from earlier here
	end
end)

Instance:GetPropertyChangedSignal() triggers when the given property is changed, and the while loop will stop running whenever its condition goes to false, so it should work just fine.

1 comment

0

Psudar I solved it by wrapping the code in an if statement inside of the while loop. That way, it then permanently checks to see if the value is true or false, then leads a line, makes it false, and then destroys itself.

This discussion is preserved as it was. New answers, comments, and votes are closed.