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

Help with loop?

So im trying to fix this,But ive got no idea how.

while true do
	wait()                                                                              
	if Player.PlayerGui:FindFirstChild("ShopGUI") then
		Player.PlayerGui.ShopGUI.ValorUltra.MouseButton1Click:connect(clicked)
	else
	
	end
end

The idea is that the loop runs infinetly,So whenever "ShopGUI" is found in his backpack it runs the function once.

Anyone help?

1 answer

✓ Accepted answer

Why would you put a connection line in an if statement?

Instead of a while loop, try the ChildAdded event:

Player.PlayerGui.ChildAdded:connect(function (YourGUI)
	if YourGUI.Name == ShopGUI then
		--[[YourFunction]]()
	else
		return nil
	end
end)

Or

Player.PlayerGui.ChildAdded:connect(function (YourGUI)
	if YourGUI.Name ~= ShopGUI then 
		return
	end
	--[[YourFunction]]()
end)

If you want the function clicked() to fire, perhaps put the whole function clicked() function and the connection line to a different script, with its parent in the ShopGUI:

function clicked()
	--[[Your Statement]]
end

script.Parent.ValorUltra.MouseButton1Click:connect(clicked)

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