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

Why won't this loop run?

I have a script that slides a frame across the screen when it turns visible.

function changed()
	local frm = script.Parent
	if frm.Visible == true then
		frm.Position = UDim2.new(0, 420, 0, 0)
		local value = script.Value.Value
		value = 0
		repeat 
			wait(0.1)
			value = value + 5
			frm.Position = UDim2.new(0, 420, 0, (value))
		until
			value == 275
			frm.Position = UDim2.new(0, 420, 0, 275)
	end
end

script.Parent.Changed:connect(changed)

The frame goes to its starting position but the loop refuses to play. The script seems fine to me; what's wrong with it?

1 answer

✓ Accepted answer

You're modifying frm in your loop, and then waiting. But whenever frm is Changed, you reset the position (line 4).

Chances are the loop is running, there are just more and more of these loops undoing each other.


One option is to not used a Changed event to do this, and instead something like

while true do
	while not frm.Visible do
		wait()
	end
	
	repeat
		....
	until value == 275
	
	while frm.Visible do
		wait()
	end
end

Alternatively, use essentially a debounce in addition to your Changed event.

local moving = false
local frm = script.Parent -- You should really be assigning this *outside* the function

function changed()
	if not moving then
		if frm.Visible then
			moving = true
			....
			local value = 0
			repeat
				....
			until value == 275
		moving = false
	end
end

frm.Changed:connect( changed )

An aside: You aren't modifying script.Value by using the variable value. There's really no reason to use script.Value at all -- the variable does all the work for you.

1 comment

0

Thanks!

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