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?
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?
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.
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.
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!
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