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)
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)
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)
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.
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.