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?
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