Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

GUI Keeps Flashing When Part Touched Problem?

Asked by 2 years ago

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)
0
I think you need a debounce ej1334 0 — 2y

1 answer

Log in to vote
0
Answered by 2 years ago

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.

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. NotThatFamouss 605 — 2y
0
it worked thanks!!!!! sorry late response loll djcraft1237 4 — 2y
0
well mostly it is fixed but it still flashes sometimes djcraft1237 4 — 2y
0
decided to unaccept it cuz the flashing is actually a problem djcraft1237 4 — 2y
Ad

Answer this question