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)
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
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)
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.