Simple Code Help

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.

Try removing the hum.Died event.

Why do you have the two events set up like that? That won’t work.

Correct way:
PlayerAdded (you can get player from this event)
CharacterAdded
Humanoid.Died

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

“Last checkpoint”, are you storing it somewhere in case they rejoin?
And yes, I am correct if you are planning to set it up.

What you want is not what I provided, but it is the main thing you need to make it.

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		local HRP = char:WaitForChild("HumanoidRootPart")
		HRP.CFrame = game.Workspace[tostring(player.leaderstats.Level.Value)]["Spawn"].CFrame + Vector3.new(0,8,0)
	end)
	player:LoadCharacter()
	local PlayerChar = player.Character or player.CharacterAdded:Wait()
	PlayerChar:WaitForChild("Humanoid").Died:Connect(function()
		player:LoadCharacter()
	end)
end)
1 Like

I am storing it using datastore 2


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)

I think this should fix your problem.

don’t think

this is the best way I thought of doing this but. The character loading time can change.

Then add a Player:LoadCharacter function.

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.

I think his script is in StarterCharacterScripts.

1 Like

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.

I think he should put it in ServerScriptService.

Nope it works in startercharacter.

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.