Changing a Player's CFrame does not work

Greetings, this Script is supposed to teleport a Player after dying to the RespawnPart. Unfortunately that does not work and I have no idea why. It checks for the creator tag because of a Sword Fighting system. I have checked if the tag existed and it indeed did.

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		repeat wait() until Player:HasAppearanceLoaded()
		if Character:FindFirstChild("creator") then
			Character:SetPrimaryPartCFrame(game.Workspace.RespawnPart.CFrame)
		end
	end)
end)
2 Likes

Try adding a wait and then setting the primary part CFrame. This is to ensure it overides the default spawn system.

1 Like

The if statement is likely returning as false, which would have the code inside it ignored. You can probably get the humanoid, and Use the Died event to check if they died.

Character.Humanoid.Died:Wait()
    -- code

-- or:

Humanoid.Died:Connect(function()
   -- code
end)
1 Like

That unfortunately did not work out, I have tried this before as well.

1 Like

That did not work I still have no idea why it doesn’t. I have included it in the script where .Died is called.

1 Like

Well, looking at your code, you are checking if the character has the creator Item, are you sure you are looking in the correct place, The creator Instance is usually placed inside the Humanoid, and that may be why its not working.

1 Like

That is correct, the other script finds this part and adds the points to a database. Just like a basic sword fighting system uses the creator tags inside of the characters.

Its Actually Inside the Characters Humanoid which is a child of the character, and not inside the Character Directly, you need to look inside the Humanoid. or do a recursive search. Like so:

Character:FindFirstChild("creator", true) -- the true makes it so it looks through the Descendants of the Character (its children)

Nope but I have just gotten this new error:

Please try using “characteradded”

local players = game:GetService("Players")
local respawnpart = game:GetService("Workspace"):WaitForChild("RespawnPart")
players.LocalPlayer.CharacterAdded:Connect(function()
	repeat wait() until players.LocalPlayer.Character:FindFirstChild("HumanoidRootPart")

	pcall(function()
		print("ADDED")
		if players.LocalPlayer.Character:FindFirstChild("creator") then
			players.LocalPlayer.Character:WaitForChild("HumanoidRootPart").CFrame = respawnpart.CFrame
		else
			print("NOT FOUND")
		end
	end)
end)

yeah, then I fixed your Issue, now you just need to add CFrame

  1. He is already using CharacterAdded

  2. A pcall is not needed for the code to function, just a simple if statement should be enough to prevent an error

You could either switch to Deferred SignalBehavior under workspace or use task.defer. I commonly use this line when parenting Characters:

task.defer(workspace.PivotTo, Character, CFrame)

You should also get rid of the repeat line. Not only does it not really help but there’s a Player.CharacterAppearanceLoaded even that you can use instead of polling.

That sadly did not change the issue.

What is your current code, do you mind showing that?

For clarification, this is a Server Script not a Local Script.

Moving the character right after spawn is a little finicky because the default code also sets the CFrame of the character.

I did some experimenting and found this was the best way to move the character without the camera flashing and the character being always moved:

-- Wait for HRP to be created and added to character
local hrp = character:WaitForChild("HumanoidRootPart")
-- Wait for HRP to be moved to Workspace along with the character
while not hrp:IsDescendantOf(Workspace) do
	hrp.AncestryChanged:Wait()
end
-- Wait one cycle to avoid race condition
task.wait()
-- Set CFrame
hrp.CFrame = newCFrame

Adding that to your code, it would look something like this:

local function onCharacterAdded(Character)
	local hrp = Character:WaitForChild("HumanoidRootPart")
	if Character:FindFirstChild("creator") then
		while not hrp:IsDescendantOf(Workspace) do
			hrp.AncestryChanged:Wait()
		end
		task.wait()
		hrp.CFrame = game.Workspace.RespawnPart.CFrame
	else
		warn("Did not find creator ValueInstance, did not set character CFrame!")
	end
end
local respawnpart = game:GetService("Workspace"):WaitForChild("RespawnPart")
game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		if Character:FindFirstChild("creator", true) then
			task.defer(workspace.PivotTo, Character, CFrame)
			Player.Character:WaitForChild("HumanoidRootPart").CFrame = respawnpart.CFrame
		end
	end)
end)

After applying it into my code that also did not seem to work.

1 Like

Huh, no warning either?

That’s strange. I’m not sure what the problem is then :confused: