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

GUI Keeps Flashing When Part Touched Problem?

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)

1 comment

0

I think you need a debounce

1 answer

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.

4 comments

1

Very well explained! I do would like to point out a few things though. You can use "not debounce" as a replacement for "debounce == false" and you should use "task.wait()" instead of the regular wait() because task.wait() does not throttle. As for the thread poster, you should use TouchEnded instead of using the repeat wait() bit.

0

it worked thanks!!!!! sorry late response loll

0

well mostly it is fixed but it still flashes sometimes

0

decided to unaccept it cuz the flashing is actually a problem

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