[Solved] CharacterAdded event fires more then once

When a player joins, he can deploy through the main menu, when the character is added the server will clone the weapons to the player’s inventory.
If the player dies, he gets respawned again, receives his weapons etc.

Now, when the player deploys again, he gets his 3 weapons added, but if he dies now, he will get after respawning each weapon twice, if the player repeats the process of returning to the main menu and deploying, and dies again it will be 3 times the same weapon.

Function that gets called to spawn the weapons in

Script
Players.PlayerAdded:Connect(function(player) -- Player:Target / Killer: Killer
	loadPlayerData(player)
	loadWeaponData(player)
	returnData(player, nil, nil)
	
	local Template = Instance.new 'BoolValue'
	Template.Name = 'leaderstats'
	Instance.new('IntValue', Template).Name = "Kills"
	Instance.new('IntValue', Template).Name = "Deaths"
	Instance.new('IntValue', Template).Name = "Level"
	task.wait(1)
	local Stats = Template:Clone()
	Stats.Parent = player
	local Deaths = Stats.Deaths
	
	player.leaderstats.Level.Value = getPlayerData(player,"Level")

	player.CharacterAdded:Connect(function(Character)
		SpawnWeapons(player)
		local Humanoid = Character:FindFirstChild "Humanoid"
		if Humanoid then
			Humanoid.Died:Connect(function()
				for i, Child in pairs(Humanoid:GetChildren()) do
					if Child:IsA('ObjectValue') and Child.Value and Child.Value:IsA('Player') then
						local Killer = Child.Value
						UpdateValues(Killer, player, "Kill")
						if Killer:FindFirstChild 'leaderstats' and Killer.leaderstats:FindFirstChild "Kills" then
							local Kills = Killer.leaderstats.Kills
							Kills.Value = Kills.Value + 1
							Deaths.Value = Deaths.Value + 1
						end
						return
					end
				end
			end)
		end
	end)
end)

Event that fires when the player character gets added

Script
local function SpawnWeapons(player: Player)
	local playerItems = wfc(ReplicatedStorage.STARTERPACK, player.Name):GetChildren()
	local Primary
	local Secondary
	local Special

	-- Organize weapons by type
	for _, v in pairs(playerItems) do
		if ffc(AllWeapons.Primary, v.Name) then
			Primary = v.Name
		elseif ffc(AllWeapons.Secondary, v.Name) then
			Secondary = v.Name
		elseif ffc(AllWeapons.Special, v.Name) then
			Special = v.Name
		end
	end

	-- Function to copy weapon
	local function copyWeapon(weaponFolder, weaponName)
		local weapon = ffc(weaponFolder, weaponName)
		if weapon then
			local clonedWeapon = weapon:Clone()
			clonedWeapon.Parent = player.Backpack
			print("Weapon Cloned")
		end
	end

	copyWeapon(AllWeapons.Primary, Primary)
	copyWeapon(AllWeapons.Secondary, Secondary)
	copyWeapon(AllWeapons.Special, Special)
	Primary = nil
	Secondary = nil
	Special = nil
end

And the code that gets executed whenever the player returns to the main menu

-- Local Script:
	if player.Character then
		task.wait(1)
		DestroyCharacter:FireServer(player)
	end
-- Server Script
DestroyCharacter.OnServerEvent:Connect(function(Player)
	if Player.Character then
		local Char = Player.Character
		Player.Character = nil
		Char:Destroy()
	end
	for i,v in pairs(Player.Backpack:GetChildren()) do
		v:Destroy()
	end
end)

Now, when I comment out the “DestroyCharacter” function in the local script, that bug doesn’t exist, but if I do so, then the player will still have a character.

So how could I fix this?, or is there somewhere else a problem with the code?

Are there any errors on the output??

No, there are no errors, in the Output, atleast none that cause the issue

Vid of the problem,
I added a print after when the Character is Added, weirdly enough, it’s being printed twice, when there is only 1 Character in workspace.

Okay, have you tried removing the line to set the character to nil?

if Player.Character then
	local Char = Player.Character
	Player.Character = nil --this one
	Char:Destroy()
end

Yes! But I have found a solution for the bug already I guess, it was because , when the Deploy button was pressed the functions were overlapping each other if the button was pressed again, which made the player spawn “more than once”

You should definitely mark your post as a solution, if it’s solved.

1 Like

Oh alright i understand, well, have a good day! :slight_smile:

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