:CharacterRemoving runs multiple times the more I reset

Every time the player resets the characterRemoving function will run as many times as i reset. For example if i reset once after joining the queue then the the function runs once, but if i were to reset again inside the queue the function would run twice.

local prompt = script.Parent.Button1.ProximityPrompt
local prompt2 = script.Parent.Button2.ProximityPrompt
local base = script.Parent.Part
local timer = script.Parent.RoundTimer.BillboardGui.TextLabel

local leavevent = game.ReplicatedStorage.Events.LeaveEvent
local arena1 = game.ReplicatedStorage.Events.ArenaFolder.ArenaEvent1

local players = game:GetService("Players")
local debris = game:GetService("Debris")

local queue = 0
local arena = 1


prompt2.Triggered:Connect(function(player)
	local root = player.Character:FindFirstChild("HumanoidRootPart")
	if queue < 2 then
		plr2 = player
		prompt2.Enabled = false
		queue += 1
		print(player, queue)
		timer.Text = (queue.."/2")
		root.Parent.Humanoid.WalkSpeed = 0
		root.Parent.Humanoid.JumpPower = 0
		root.Parent.Humanoid.AutoRotate = false
		root.CFrame = base.CFrame:ToWorldSpace(CFrame.new(7.5, 1, 0))
		root.CFrame = CFrame.lookAt(root.Position, script.Parent.Button1.Position)
		leavevent:FireClient(players:GetPlayerFromCharacter(root.Parent), arena)
		local playerplace = Instance.new("IntValue", player)
		playerplace.Name = "PlayerPlace"
		playerplace.Value = 2
	end
	player.CharacterRemoving:Connect(function(char)
		queue -= 1
		prompt2.Enabled = true
		timer.Text = (queue.."/2")
		print(plr2, queue)
	end)
end)
4 Likes

the connection is never disconnected so it will stack on top of itself every time you enter the queue.

you can make the connection disconnect itself like so:

local connection
connection = player.CharacterRemoving:Connect(function(char)
 --do stuff
 connection:Disconnect()
end)
3 Likes

If you replace :Connect( with :Once(, it will automatically disconnect after running one time.

3 Likes

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