Question about performance

Hello, I have a question about performance (mostly memory usage).
Recently, I made a tree that can be cut using a proximity prompt. I’m not a very good scripter, but this is the current script for it.

local tree = script.Parent.Parent
local debounce = false
local prox = script.Parent.ProximityPrompt
prox.Triggered:Connect(function(plr)
	if debounce == false then
		local stats = plr:findFirstChild("Res")
		local Wood = stats:findFirstChild("Wood")
		local stats2 = plr:FindFirstChild("leaderstats")
		local XP = stats2:FindFirstChild("XP")
		local toCollect = tree:GetAttribute("WoodCollection")
		local XPtoCollect = tree:GetAttribute("XPCollection")
		Wood.Value = Wood.Value + toCollect
		XP.Value = XP.Value + XPtoCollect
		tree.Parent = game.ServerStorage.Resources

		debounce = true
		wait(40)
		tree.Parent = game.Workspace.Resources
		debounce = false
	end
end)

I was wondering, do i need to disconnect the connection to make the script more efficient? (the tree can be cut as many times as the players want (every 40 seconds). If yes, how would I do that?

  • Yes, you should disconnect it.
  • No, it won’t make any difference as it is.

0 voters

Please tell me if this is the wrong category or something, since I rarely post.

the code isnt run without the prompt triggering so its not taking memory most of the time. disconnecting-reconnecting also means you have to run more code

but if you make connections inside connections, disconnect those so they dont stack over time, like this

someEvent:Connect(function()
	-- your code

	local newConnection
	newConnection = someOtherEvent:Connect(function()
		newConnection:Disconnect()

		-- your other code
	end)
end)
1 Like