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

How do I make GUIs located in the torsos turn off after turning on?

Asked by 2 years ago
Edited 2 years ago

Hey all, I'm trying to make a script that turns on and off a GUI that is located in the Torso of a player's character. This is what I have so far.

Local Script:

local RS = game:GetService('ReplicatedStorage')

local rEvent = RS:FindFirstChild('metaOnEvent')

local rEventOff = RS:FindFirstChild('metaOffEvent')

local meta = false

local debounce = false

local reset_Secs = 2

script.Parent.MouseButton1Down:Connect(function()

    if not meta then

        if debounce then return end

        debounce = true

        --rEvent:FireServer()

        --[[Start of newly implemented]]--

        local Players = game.Players:GetPlayers()

        for _,v in pairs(Players) do

            v.Character.HumanoidRootPart.metaGUI.Enabled = true

        end

        game.Players.LocalPlayer.Character.HumanoidRootPart.metaGUI.Enabled = false

        --[[End of newly implemented]]--

        wait(reset_Secs)

        debounce = false


    else

        meta = false

        --rEventOff:FireServer()

        --[[Start of newly implemeted]]--

        local plrs= game.Players:GetPlayers()

        for _,v in pairs(plrs) do

            v.Character.HumanoidRootPart.metaGUI.Enabled = false

        end

        --[[End of newly implemented]]--

    end

end)

But, after a player turns them on, they cannot be turn off again! Are there any solutions you guys can think up?

1 answer

Log in to vote
1
Answered by
imKirda 4491 Moderation Voter Community Moderator
2 years ago
Edited by Xapelize 2 years ago

You aren't setting meta variable to true when player enables the GUI.

local RS = game:GetService('ReplicatedStorage')

local rEvent = RS:FindFirstChild('metaOnEvent')

local rEventOff = RS:FindFirstChild('metaOffEvent')

local meta = false

local debounce = false

local reset_Secs = 2

script.Parent.MouseButton1Down:Connect(function()

    if not meta then

        if debounce then return end

        debounce = true

        --rEvent:FireServer()

        --[[Start of newly implemented]]--

        local Players = game.Players:GetPlayers()

        for _,v in ipairs(Players) do

            v.Character.HumanoidRootPart.metaGUI.Enabled = true

        end

        game.Players.LocalPlayer.Character.HumanoidRootPart.metaGUI.Enabled = false

        meta = true -- <<<<<<<<<<<<<<<<<<<<<<<< new line here

        --[[End of newly implemented]]--

        -- forget about "wait()", start using "task.wait()" it's a newer version
        -- that does not have many changes but it has better performance
        task.wait(reset_Secs)

        debounce = false


    else

        meta = false

        --rEventOff:FireServer()

        --[[Start of newly implemeted]]--

        local plrs= game.Players:GetPlayers()

        for _,v in ipairs(plrs) do

            v.Character.HumanoidRootPart.metaGUI.Enabled = false

        end

        --[[End of newly implemented]]--

    end

end)

Read about ipairs here. In this case it fits the most so I used it, since :GetPlayers returns an array, it improves performance

Ad

Answer this question