So sometimes in my code I put an event inside of an mouse click event because I want the event to be able to run only after the player clicks. Here is the problem. After the event it triggered once, I want it to reset so it can no longer be triggered again until the player clicks again. I was able to achieve this by having the script disable itself and an external script re-enable it. This works but it seems very inefficient. Any ideas on another way to do this? Here is the code if you need it.
local button = script.Parent
local player = script.Parent.Parent.Parent.Parent.Parent
local playerbutton = player.PlayerGui
local QueueModule = require(game.ServerScriptService:WaitForChild("Queue"))
button.MouseButton1Click:Connect(function()
local GUI = game.ServerStorage.QueueGUIs.QueueSettings:Clone()
GUI.Parent = playerbutton
game.ReplicatedStorage.GUIEvents.CreateQueue.OnServerEvent:Connect(function(player, minLevel, avalability)
QueueModule.LoadNewQueue(player, minLevel, avalability, script.Parent.Parent.ScrollingFrame)
script.Disabled = true
end)
end)
Once the script has disabled itself on line 13 I have a script with this code to re-enable it
while true do
wait()
if script.Parent.Script.Disabled == true then
script.Parent.Script.Disabled = false
end
end
local button = script.Parent
local player = script.Parent.Parent.Parent.Parent.Parent
local playerbutton = player.PlayerGui
local QueueModule = require(game.ServerScriptService:WaitForChild("Queue"))
local cat = nil
function newconnect()
cat = button.MouseButton1Click:Connect(function()
local GUI = game.ServerStorage.QueueGUIs.QueueSettings:Clone()
GUI.Parent = playerbutton
cat:disconnect()
cat = nil
game.ReplicatedStorage.GUIEvents.CreateQueue.OnServerEvent:Connect(function(player, minLevel, avalability)
QueueModule.LoadNewQueue(player, minLevel, avalability, script.Parent.Parent.ScrollingFrame)
end)
end)
end
newconnect()
-- you can just run this to reset the button (i think)
As you click on the button, you regenerate the event, hence in this case, OnServerEvent. There’s no reason to put it in there, as you can place it outside of your MouseButton1Click event without a change.
When it’s outside of the other event, it’ll listen to the RemoteEvent once.
But I want the OnServerEvent to only activate once, and after the player has clicked the button. and then reset itself so it will only fire again if the button is pressed again.
What do you mean that? It can’t “reset” itself - you replicate it, therefore, there will be more OnServerEvents. This event specifically listens, and fires, when called with :FireServer() - that happens once, so there’s no need to reset it.
If you want to ensure the player has done certain things like checking if they had pressed the button, use booleans for that.
If by resetting, if you mean that you don’t want it to be used anymore, utilise connection:Disconnect(). Besides, if you want to keep those OnServerEvents in a replicated area, you can trigger the event for no longer use.