This code works fine but the problem I get is when I touch the part that triggers the function, it sometimes flashes the GUI (by that i mean the gui is being repeatedly showing and stop showing itself for a while)
I notice it happens on the edges of the part that triggers the function.
Code Below:
function touch(hit) if game.Players:findFirstChild(hit.Parent.Name) ~= nil then player = game.Players[hit.Parent.Name] if player.PlayerGui:findFirstChild("ShopSetupGUI") == nil then gui = script.ShopSetupGUI:clone() gui.Parent = player.PlayerGui end repeat wait() until (player.Character.LeftFoot.Position - script.Parent.Parent.Pad.Position).Magnitude > 8 gui:Remove() end end script.Parent.Parent.Pad.Touched:Connect(touch)
This can be fixed with a debounce: A debounce is a basic coding term for a variable meant to stop something from being called a ton. It's really simple to make one, put this at the start of your code: local debounce = false
, then on the first if-statement add and debounce == false
so it looks like this if game.Players:findFirstChild(hit.Parent.Name) ~= nil and debounce == false then
. Finally right before the if-statement closes add
debounce = true wait(4) --Change this to the number of seconds you want the debounce to wait for debounce = false
The finished code would look like this
function touch(hit) if game.Players:findFirstChild(hit.Parent.Name) ~= nil and debounce == false then player = game.Players[hit.Parent.Name] if player.PlayerGui:findFirstChild("ShopSetupGUI") == nil then gui = script.ShopSetupGUI:clone() gui.Parent = player.PlayerGui end repeat wait() until (player.Character.LeftFoot.Position - script.Parent.Parent.Pad.Position).Magnitude > 8 gui:Remove() debounce = true wait(4) debounce = false end end script.Parent.Parent.Pad.Touched:Connect(touch)
Hope this helped, sorry it got a bit long. If it solved your problem please mark it as the answer. If it didn't work just comment on this and I'll see what I can do.