Help with teleporting player on respawn

So basically I have instant respawn. When the player dies I want to teleport them to the spawn part.

I can’t let the spawn point just spawn them because there is an invisible object around the spawn point and my character always spawns on top of it (Which isn’t what I want.)

So I tried making a system to immediately just teleport them to the spawn object when they respawn (Only if their team is night guard.) I put a local script inside of StarterPlayerScripts:

local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

player.CharacterAdded:Connect(function()
	print(character)
	print(player)
	if player.Team.Name == "Night Guard" then
		repeat wait() until character.Torso ~= nil
		character.Torso.CFrame = game.Workspace.Spawns["Night Guard"].CFrame
		print("Teleported")
	end
end)

What it printed out is:

  • RichPerson03return
  • RichPerson03return
  • Teleported

So the script works but it doesn’t teleport, why?

1 Like

One thing you could try is use

:PivotTo()

to move the whole character to the cframe you want

And how exactly, because that is completely new to me?

you would just do

character:PivotTo(part.CFrame)

It actually teleports the player to your position, but right after that Roblox teleports it again to the default spawn position, you can fix that by waiting one or two frames before teleporting.

local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

player.CharacterAdded:Connect(function()
	print(character)
	print(player)
	if player.Team.Name == "Night Guard" then
		local torso = character:WaitForChild("Torso")
        task.wait() -- wait for one frame, if you want to be really sure that he gets teleported to your place, try doing task.wait(0.1)
		torso.CFrame = game.Workspace.Spawns["Night Guard"].CFrame
		print("Teleported")
	end
end)
1 Like

Yeah I thought that was the problem too, but the task.wait() didn’t work after testing thrice. It didn’t print any errors though.

local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

player.CharacterAdded:Connect(function()
	print(character)
	print(player)
	if player.Team.Name == "Night Guard" then
		local torso = character:WaitForChild("Torso")
		task.wait(0.1)
		torso.CFrame = game.Workspace.Spawns["Night Guard"].CFrame
		print("Teleported")
	end
end)

First of, if you want every player to see the change then put the whole thing inside a Server Script and as for the actual teleporting part I suggest doing something like this:

local rootPart = char:WaitForChild("HumanoidRootPart") --get player's root part because everything is attached to it
rootPart.CFrame = game.Workspace.Spawns["Night Guard"].CFrame

Together it should look something like this:

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		if player.Team.Name == "Night Guard" then
			local rootPart = char:WaitForChild("HumanoidRootPart") --get player's root part because everything is attached to it
			rootPart.CFrame = game.Workspace.Spawns["Night Guard"].CFrame
			print("Teleported")
		end
	end)
end)

Local scripts in StarterPlayerScripts run only once so your character variable defined at the start of the script does not update when you respawn, then you are trying to teleport the old character. Instead, CharacterAdded passes the new character parameter, use it.

local player = game:GetService("Players").LocalPlayer

player.CharacterAdded:Connect(function(character)
	print(character)
	print(player)
	if player.Team.Name == "Night Guard" then
		local torso = character:WaitForChild("Torso")
		task.wait(0.1)
		torso.CFrame = game.Workspace.Spawns["Night Guard"].CFrame
		print("Teleported")
	end
end)
1 Like

First of all, putting it in a server script won’t work. I only want the script to run every time the LocalPlayer dies, and also that script will surely break with it running everything at once every time a player joins or dies.

And I’m pretty sure the server can always see where your player is at, and it doesn’t matter if you teleport them using a local script. I’ve seen it work like that with walk speed. And it doesn’t matter if I use a different method to teleport. I’m pretty sure the problem is what you said before.

It works, but for some reason there is a really really small chance it doesn’t work sometimes.

Try increasing the wait time to 0.3+ seconds.

2 Likes

I already figured it out. I put a kill brick where the player spawns above the invisible part, so if they ever do spawn up there it will imminently kill them. It’s so fast you don’t even see it happen. I might add loading screen to cover it up though.

This doesn’t work