Character.childadded not working after resetting

A script I am using to test whether or not a tool was added to the player’s character seems to break and not work after the player resets. The script works when the player first joins though. Does this have something to do with the player’s character being allocated to different memory after resetting or some other issue?

player.Character.ChildAdded:Connect(function(child)
	if child.ClassName == "Tool" then
		--reset()
		script:SetAttribute("IsEquiped", true)
		if child:GetAttribute("Type") then
			script:SetAttribute("Type", child:GetAttribute("Type"))
		end
		if child:GetAttribute("Type") == "Melee" then
			script:SetAttribute("AttackCooldown", child:GetAttribute("AttackCooldown"))
		end
	end
end)

1 Like

Is this a local script in starter player scripts? If it is then move it to startercharacterscripts.

The reason it works the first time is because you index the character and make a connection for it, it works perfectly fine. But when you reset the player will spawn with a new character and that connection you made is still tied to the old character that you indexed on the first join. So you should follow what the reply above said and move that script to StarterCharacterScripts instead

Hi!

You need to listen to the CharacterAdded Event, then listening to the ChildAdded Event like below:

player.CharacterAdded:Connect(function(char)
    char.ChildAdded:Connect(function(child)
        if child.ClassName == "Tool" then
            --reset()
            script:SetAttribute("IsEquiped", true)
            if child:GetAttribute("Type") then
                script:SetAttribute("Type", child:GetAttribute("Type"))
            end
            if child:GetAttribute("Type") == "Melee" then
                script:SetAttribute("AttackCooldown", child:GetAttribute("AttackCooldown"))
            end
        end
    end)
end)

Hope this helps!

1 Like

Put it in StarterCharacterScripts.

This worked thank you!
I didn’t know you could put two event connect functions into each other.

Note that this is a local script in player scripts. Another note that I did not want to have this script in the player’s character because I did not want it to reset every time the player respawned (this was not the entire script).

If your problem solved, please mark the respective reply as the solution, so people don’t waste their time trying to figure out if there’s a solution or not.