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

Why does my GUI .visible script work only once after I touch the part?

Asked by 4 years ago
debounce = false

workspace.Island.Teleport.IslandTP1.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid")  and debounce == false then
    local plr = game.Players:GetPlayerFromCharacter(hit.Parent) 
    local GUI = plr.PlayerGui.ScreenGui:FindFirstChild("Frame")
    debounce = true
    if GUI.Visible == false then
        GUI.Visible = true  
    wait(1)
    debounce = false
    end
    end
end)

I touch the part it shows me the gui but after I make my gui invisible it doesnt work unless I reset.

2 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

So for your if statement you are not putting an else. If statements can have more than one condition or statements built inside of it. The reason why it works once is because your saying if the gui is false then set to true.

So your script fixed would be:

debounce = false

workspace.Island.Teleport.IslandTP1.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid")  and debounce == false then
        local plr = game.Players:GetPlayerFromCharacter(hit.Parent) 
        local GUI = plr.PlayerGui.ScreenGui:FindFirstChild("Frame")

        debounce = true
        if GUI.Visible == false then
            GUI.Visible = true  
    else
        GUI.Visible = false
    end

    wait(1)
    debounce = false
   end
end)

If it doesn't work leave a comment on my question with the output error in it and I can try to solve it.

If you want here is a link to the roblox wiki on if statements: https://developer.roblox.com/en-us/articles/Conditional-Statements-in-Lua

You can ask clarification anytime

0
Thank you! MaciBoss1950 16 — 4y
0
No problem! 123nabilben123 499 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

It happens when your GUI is visible and "Touched" event occurs (may happen when character moves on the teleport platform). In this case the debounce flag can't reset. Just put the debounce = false line outside of the if block. If you're trying to sync it with the other script using a wait function - don't do it. It's absolutely inreliable.

debounce = false

workspace.Island.Teleport.IslandTP1.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid")  and debounce == false then
    local plr = game.Players:GetPlayerFromCharacter(hit.Parent) 
    local GUI = plr.PlayerGui.ScreenGui:FindFirstChild("Frame")
    debounce = true
    if GUI.Visible == false then
        GUI.Visible = true  
        wait(1)
    end
    debounce = false
    end
end)

Answer this question