So ive created a respawn script that loads the character after death. The script is local (located in the StarterPlayerScripts folder).
player = game.Players.LocalPlayer function PlayerDied() wait(2) player:LoadCharacter() player.Character.Torso.CFrame = CFrame.new(0,20,0) end player.Character.Humanoid.Died:connect(PlayerDied)
The only problem is that after the character is loaded and his/her position is set to a specified location, the player is teleported to the players defualt spawn location. There are no direct errors, instead I need away to prevent the player from being teleported to the spawn. If you dont understand, please test this for yourself.
Your problem sadly goes deeper than the CFraming Torso Position. In the Notes of the LoadCharacter wiki page it states;
- This item should be used in a Script to work as expected online.
So you're going to have to make a Server Side script.
For the final solution script we want to change the CharacterAutoLoads
property to false.
Once completed with that the PlayerAdded
event will need to be connected to.
We will want to take the Player and connect them to the CharacterAdded
event, that way we can connect to their humanoid when we first go to load their character.
In the CharacterAdded
function we will want to make sure Humanoid loads, so we'll have a repeat loop keep waiting until Humanoid does exist.
Once we find out that Humanoid exists, then we'll connect it to the Died
event.
When the Died
event fires, the script will wait the two seconds you desired and then load the character.
Once done with that we'll want to see if the Character's Torso exists so we will establish a repeat loop again to wait until it loads. You will then want to change the CFrame position of the Torso.
Also, do keep in mind that CFrame requires Vector3 values, so you would want to encase the values in Vector3.new()
. It will look something like in the script below.
game:GetService('Players').CharacterAutoLoads = false game:GetService('Players').PlayerAdded:connect(function(Player) Player.CharacterAdded:connect(function(Character) repeat wait() until Character:FindFirstChild('Humanoid') Character.Humanoid.Died:connect(function() wait(2) --Prefered time. Player:LoadCharacter() repeat wait() until Player.Character:FindFirstChild('Torso') Player.Character.Torso.CFrame = CFrame.new(Vector3.new(0,20,0)) end) end) Player:LoadCharacter() --When the player joins the game, this will be the first time they load. end)