Lacking capability writePlayer

I am making a script where the player will respawn as a ghost whenever they die, but whenever I try to test it, I get the error “The current thread cannot set ‘Character’s name’ (lacking capability WritePlayer)” and doesnt work

local ghost = game.ServerStorage.Ghost
game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		local human = char:WaitForChild("Humanoid")
		human.Died:Connect(function()
			plr.CharacterAdded:Connect(function(char)
				ghost.Name = plr.Name
				plr.Character = ghost
				
				local clone = ghost:Clone()
				local rootPart = ghost:FindFirstChild("HumanoidRootPart") or ghost:FindFirstChild("Torso")
				local plrRoot = char:FindFirstChild("HumanoidRootPart") or char:FindFirstChild("Torso")
				
				if rootPart and plrRoot then
					rootPart.CFrame = plrRoot.CFrame
					
					clone.Parent = workspace
				end
			end)
		end)
	end)
end)

You have a memory leak. The player’s character is being repeatedly reassigned to ghost, so sometimes the ghost is already the player’s character and can’t be renamed. Remove the duplicate PlayerAdded function, and also the humanoid.Died function, or disconnect old functions.

try :Once() instead of connect to solve the memory leak first

also you got few mistakes

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Once(function(char)
		local human = char:WaitForChild("Humanoid")
		human.Died:Once(function()
			plr.CharacterAdded:Once(function(char)
				

				ghost.Name = plr.Name
				local clone = ghost:Clone()
				plr.Character = clone
				local rootPart = clone:FindFirstChild("HumanoidRootPart") or ghost:FindFirstChild("Torso")
				local plrRoot = char:FindFirstChild("HumanoidRootPart") or char:FindFirstChild("Torso")
				
				if rootPart and plrRoot then
					rootPart.CFrame = plrRoot.CFrame
					clone.Parent = workspace
				end
			end)
		end)
	end)
end)

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