Tool scripts break on reset

Hello! I have a problem.

When resetting in the game, tools break. They work the first time joining, but after that they don’t work.

Changing Character script:

function newGuest(player)
	local newChar = game.ServerStorage.DefaultGuest:Clone()
	local plrChar = player.Character
	newChar.PrimaryPart = newChar.HumanoidRootPart
	newChar:SetPrimaryPartCFrame(plrChar.PrimaryPart.CFrame)
	newChar.Name = player.Name
	for i, scri in pairs(plrChar:GetChildren()) do
		if scri:IsA("Script") or scri:IsA("LocalScript") then
			scri.Parent = newChar
		end
	end
	player.Character = newChar
	local rootPart = newChar:FindFirstChild("HumanoidRootPart")
	local plrRoot = player.Character:FindFirstChild("HumanoidRootPart")
	if rootPart and plrRoot then
		rootPart.CFrame = plrRoot.CFrame
	end
	newChar.Parent = game.Workspace
	for i, part in pairs(newChar:GetDescendants()) do
		if part:IsA("BasePart") then
			part.Anchored = false
		end
	end
	wait(0.3)
	for i, scri in pairs(newChar:GetChildren()) do
		if scri:IsA("Script") or scri:IsA("LocalScript") then
			scri.Enabled = true
		end
	end
end

game.Players.PlayerAdded:Connect(function(player)
	local guest = robloxCore.RobloxCoreRemotes:WaitForChild("GetGuest"):InvokeClient(player)
	
	if guest == true then
		local tag = Instance.new("NumberValue", player)
		tag.Name = "GuestID"
		tag.Value = math.random(1, 9999)
		
		newGuest(player)
		
		player.CharacterAdded:Connect(function()
			newGuest(player)
		end)
	else
		wait(0.3)
		player.CharacterAdded:Connect(function(char)
			for i, scri in pairs(char:GetChildren()) do
				if scri:IsA("Script") or scri:IsA("LocalScript") then
					scri.Enabled = true
				end
			end
		end)
	end
end)

Why does this happen? And what’s a possible solution?

3 Likes

Can you show a video of the problem happening?

2 Likes

Thanks, by looking at your video, I assume that, the sword works before you reset the chracter, and then the sword doesn’t work anymore after you reset your character.

The code you have provided in this post doesn’t really help much as I assume this code functions to replace the player’s default character model to a pre-existing one you have added in the game.

Can you show me the sword scripts? Or is this sword the default Roblox sword which you have obtained from the Toolbox?

Hey.
Yes, the sword is the Classic Sword from the toolbox, and the apple is also from the toolbox.

Hello, sorry for the late reply. I managed to reproduce the problem in studio.

So far I cannot find any solutions yet, however, through your video and my findings, it seems like Player.CharacterAdded event runs a lot of times after the player resets. I am still trying to find out what’s wrong with this script.

1 Like

I see… that certainly is concerning, shouldn’t it only fire once?

It should. But for some reason, only your script fires a lot of times. I think it has something to do with how you replace the player’s character model.

1 Like

Unfortunately due to time constraints, I cannot continue helping you. However, what I can tell you is that the way you are spawning the player with a new character model is wrong, causing the event to fire many times and your sword to fail. (Turns out, the server script of the sword tool doesn’t run at all, that’s why you can’t use your sword.)

Why not try watching this tutorial and see if you can apply their concept into your idea?

Fortunately, I found a solution.
Adding a debounce to the code worked, and has made the game smoother in every way, thank you for taking your time to help me!

2 Likes

No problem. Can I see how you add the debounce? I tried to do the same, where I add a debounce but it didn’t work for me.

local debounce = false

function newGuest(player)
	if debounce then return end
	
	debounce = true
	
	local newChar = game.ServerStorage.DefaultGuest:Clone()
	local plrChar = player.Character
	newChar.PrimaryPart = newChar.HumanoidRootPart
	newChar:SetPrimaryPartCFrame(plrChar.PrimaryPart.CFrame)
	newChar.Name = player.Name
	for i, scri in pairs(plrChar:GetChildren()) do
		if scri:IsA("Script") or scri:IsA("LocalScript") then
			scri.Parent = newChar
		end
	end
	player.Character = newChar
	local rootPart = newChar:FindFirstChild("HumanoidRootPart")
	local plrRoot = player.Character:FindFirstChild("HumanoidRootPart")
	if rootPart and plrRoot then
		rootPart.CFrame = plrRoot.CFrame
	end
	newChar.Parent = game.Workspace
	for i, part in pairs(newChar:GetDescendants()) do
		if part:IsA("BasePart") then
			part.Anchored = false
		end
	end
	wait(0.3)
	for i, scri in pairs(newChar:GetChildren()) do
		if scri:IsA("Script") or scri:IsA("LocalScript") then
			scri.Enabled = true
		end
	end
	debounce = false
end
1 Like

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