Is this good or bad?

I came up with this script which basically assigns an Id to each connection that I want control of, so Touched, CharacterAdded ecc Events on roblox.

for _, Part in ipairs(Checkpoints) do
	if Part:IsA("Part") and tonumber(Part.Name) then
		Connections[UserId] = Part.Touched:Connect(function(Hit)
			local player = game.Players:GetPlayerFromCharacter(Hit.Parent)
			if player then
				local leaderstats = player:FindFirstChild("leaderstats")
				local stage = leaderstats and leaderstats:FindFirstChild("Stage")
				if stage and stage.Value == tonumber(Part.Name) - 1 then
					stage.Value += 1
				end
			end
		end)
	end
end

Is this bad since it makes a new connection per player? I wanna know I’ve asked a few AI Models and I got told that it is not good practive to make connections per player but I wanna hear the opinion from others.

Why would you want a connection per player for specifically this script? Pretty sure it would work perfectly fine with a single Touched connection for each part

To disconnect them later when the player leaves.

Why not just make one connection for each part and not disconnecting them?

Fair enough I was thinking about that but I was worried a bit.

What are you worried about exactly

I was trying to fix some memory leaks.

Im assuming these checkpoints are persistent and dont get destroyed, in which case there shouldn’t be any problem with having a few connections. If the parts does get destroyed however, the connections will automatically be disconnected when the Destroy() is called

1 Like

Alright thank you, have a nice day.

1 Like

Same to you, have a nice day and good luck scripting

1 Like

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