Function variable stacking onto each other every time it is run?

I am trying to figure out why my function keeps “holding variables” from past times like somehow the connections were being connected multiple times and never disconnecting,

The code snippet below is partially how the collection system in my game works. When a remote is sent from the server to a local function it then runs a main function to deal with the rest to start a dialogue and wait for a close button, when the player presses the close button it should delete the phrog and close the ScreenGui for the dialogue, but somehow if I ask the close function to print its phrog it prints an old one whereas if i ask it to print the phrog outside of the close function it prints the correct one always, I do not know how or why this is happening and if needed I can try to explain my script more

If anyone knows how to reset or disconnect a function after it is run that would help, I tried returning

-- 
local function ClientPhrogFunction(player, Phrog)

	local DisconnectClose = ScreenGui.BackGround.CloseButton.MouseButton1Click:Connect(function()
		Phrog:Destroy()
		debounce = true
		warn("end")
		warn(Phrog)
	end)
	
	ScreenGui.BackGround.CloseButton.MouseButton1Click:Connect(function()
		task.wait(3)
		DisconnectClose:Disconnect()
		warn("disconnect")
		warn(Phrog)
	end)
end
1 Like

You’re connecting two listeners to the same event, can I ask why that is because it’s not something I would do in this case?


You’re disconnecting after 3 seconds, and you’re not disconnecting the second connection. Why?


Since your client code doesn’t handle more then 1 call coming in properly, does your server code have checks in place to make sure it only gets called once?


WDYM it “collects phrog variables”? What does it mean to “stack on top of each other”?

1 Like

Sorry for not replying quickly I posted this very very late at night. The server only sends one call to the local script through a debounce, in the heavily simplified code below somehow the connected click function prints a different Phrog than what is given by the function after any 2nd time being used and I have no idea HOW

local function ClientPhrogFunction(player, Phrog)
  warn(Phrog) -- this would always print the correct phrog

  ScreenGui.BackGround.CloseButton.MouseButton1Click:Connect(function()

     ScreenGui.Enabled = false
     Phrog:Destroy()

     warn(Phrog) -- this would print the correct phrog the first time but any collected after would print the last one SOMEHOW
  end

end

in the first script, I was trying to see if I could disconnect the function because it was somehow printing an older variable(sometimes every old one) but I was having a lot of trouble with disconnecting the functions and if I was even looking in the right place originally…

note that this function is sent by an OnClientEvent function in the local script
I have also found a dev post similar to my issue but it’s on a server script Function / RemoteEvent running more each time it's being executed - #2 by hollaquetalBRUH

1 Like

It turns out that if you attach the function to a nil variable you could :Disconnect() from the inside and this solved the issue.

local CloseDisconnect = nil
		
CloseDisconnect = ScreenGui.BackGround.CloseButton.MouseButton1Click:Connect(function()
	ScreenGui.Enabled = false
	VisiblePlayer()
	Phrog:Destroy()
	debounce = true
	CloseDisconnect:Disconnect()
end)

(Thanks to GrognakThePizza)

1 Like

I also want to note that there’s a better official way of doing this:

-- RBXScriptSignal::Once connects to a signal one time and automatically disconnects when it fires
ScreenGui.BackGround.CloseButton.MouseButton1Click:Once(function()
	ScreenGui.Enabled = false
	VisiblePlayer()
	Phrog:Destroy()
	debounce = true
end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.