local char = script.Parent
local hum = char:WaitForChild("Humanoid")
local plr = game.Players:GetPlayerFromCharacter(char)
char.PrimaryPart.CFrame = workspace:FindFirstChild(tostring(plr.leaderstats.Level.Value)):FindFirstChild("Spawn").CFrame
hum.Died:Connect(function()
plr.CharacterAdded:Connect(function()
char.PrimaryPart.CFrame = workspace:FindFirstChild(tostring(plr.leaderstats.Level.Value)):FindFirstChild("Spawn").CFrame
end)
end)
Why is this code not working it’s so simple I tried other things to make it work but it still didn’t work. The code is supposed to spawn the player at the place they left. and after they die it supposed to spawn them at the last checpoint but it’s not working.
nope that’s not what I want. Player Joins The game → gets teleported to their last checkpoint → and if they die → they get teleported to their last checkpoint
local char = script.Parent
local hum = char:WaitForChild("Humanoid")
local plr = game.Players:GetPlayerFromCharacter(char)
char.PrimaryPart.CFrame = workspace:FindFirstChild(tostring(plr.leaderstats.Level.Value)):FindFirstChild("Spawn").CFrame
hum.Died:Connect(function()
wait(7) -- waits for the character to load in again.
char.PrimaryPart.CFrame = workspace:FindFirstChild(tostring(plr.leaderstats.Level.Value)):FindFirstChild("Spawn").CFrame
end)
If this is in PlayerScripts, then you need to account for the character being reset.
i.e., local char = script.Parent doesn’t work (and I’m pretty sure script.Parent isn’t the player’s character if it’s in PlayerScripts) when the player dies since the character you are referencing is the old one. That means when you set the CFrame, it doesn’t move the new player - it moves the old player (which doesn’t exist).
Ideally you would want to call the CharacterAdded event,
Player.CharacterAdded(function(NewChar)
-- set cframe here
end)
Taking into account that the new character is passed as an argument. That way you can set the CFrame of the new character.
Also note that you need to get the new humanoid too.
If it’s in StarterCharacter then it won’t even call the function, the script gets destroyed when the player dies (which means the CharacterAdded event inside the script doesn’t recieve the event). Either way there is an issue.
Here’s some pseudo code, hope it helps out a bit.
Don’t expect it to work without some changes.
local Players = game:GetService('Players')
local Workspace = game:GetService('Workspace')
local StagesFolder = Workspace.Stages
local function TeleportToStage(Character, Stage)
local Humanoid = Character:FindFirstChild('Humanoid')
repeat wait() until Humanoid -- due to some teleportation issues that roblox handles on-spawn,
-- this seems to be the best solution to handle it and avoid issues.
if Character.PrimaryPart then
Character:SetPrimaryPartCFrame(StagesFolder[Stage.Value].CFrame + Vector3.new(0, 2, 0)) -- X, Y, Z
-- add some Y-vector so you don't spawn inside the spawn location.
end
end
Players.PlayerAdded:Connect(function(Player)
local Config = Instance.new('Configuration')
Config.Name = 'leaderstats'
Config.Parent = Player
local Stage = Instance.new('IntValue')
Stage.Name = 'Stage'
local StageSave = DataStore2Location('Stages', Player)
Stage.Value = StageSave:Get(0)
Stage.Parent = Config
local TempChar = Player.Character or Player.CharacterAdded:Wait()
TeleportToStage(TempChar, Stage.Value)
-- in case the player doesn't pass CharcterAdded as we're loading their data above.
Player.CharacterAdded:Connect(function(Char)
TeleportToStage(Char, Stage.Value)
end)
end)
It is much easier to handle everything in one script.