Teleporting at the beginning rather at your current stage

Trying to make it so when a player joins, they spawn at their current stage. As of now, It’ll just teleport you to the beggining (the spawn point). An error I got while trying to make this happen is: [ServerScriptService.Library.Datastore:36: attempt to index nil with ‘Character’]"

Anyone know how to fix this? Here’s the script:

game.Players.PlayerAdded:Connect(function(player)
	local key = "player-" .. player.UserId
	
	local GetSave = DataStore:GetAsync(key) -- check for existing data 
	
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	
	local hidden = Instance.new("Folder", player)
	hidden.Name = "hidden"
	
	local checkpoint = Instance.new("IntValue", leaderstats)
	checkpoint.Name = "Stage"
	
	local spawnpoint = Instance.new("IntValue", hidden)
	spawnpoint.Name = "SpawnPoint"
	
	if GetSave then
		checkpoint.Value = GetSave
		print("Data Loaded For " .. player.Name)
	else
		checkpoint.Value = 1
		print("New Data Created For " .. player.Name)
	end
	
	local player = game.Players.LocalPlayer
	local character = player.Character
	local respawn = cpFolder[player.leaderstats.Stage.Value]
	character.HumanoidRootPart.CFrame = respawn.CFrame + Vector3.new(0,3,0)
	
	player.CharacterAdded:Connect(function(character)
		repeat wait() until workspace:FindFirstChild(character.Name)

		local player = game.Players:GetPlayerFromCharacter(character)
		local respawn = cpFolder[player.hidden.SpawnPoint.Value]

		character.HumanoidRootPart.CFrame = respawn.CFrame + Vector3.new(0,3,0)
	end)

end)
1 Like

Which line specifically is having the error?

1 Like

This is the line that is erroring:

1 Like

Try

local character = player.Character or player.CharacterAdded:wait()

2 Likes

I still get the error on that same line.

1 Like

You have this in Server Script Service but you are using

local player = game.Players.LocalPlayer

which can only be used in a local script

1 Like

you don’t actually need that line of code because player is passed to the function at the beginning:

game.Players.PlayerAdded:Connect(function(player)

1 Like

I do because without the first line of this:

	local character = player.CharacterAdded:wait()
	local respawn = cpFolder[player.leaderstats.Stage.Value]
	character.HumanoidRootPart.CFrame = respawn.CFrame + Vector3.new(0,3,0)

It won’t work for the last line if I delete that line. Unless you mean a different line.

1 Like

You always want to try to get services using GetService

local LocalPlayer = game:GetService("Players").LocalPlayer
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
1 Like

that’s the line I was talking about that you don’t need.
Calling Players.LocalPlayer from the server will result in nil
the function is passed the player so you don’t need to define it again. Your script should work if you remove the ‘local player=game.Players.LocalPlayer’ line

I did:

game.Players.PlayerAdded:Connect(function(player)
	local key = "player-" .. player.UserId
	
	local GetSave = DataStore:GetAsync(key) -- check for existing data 
	
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	
	local hidden = Instance.new("Folder", player)
	hidden.Name = "hidden"
	
	local checkpoint = Instance.new("IntValue", leaderstats)
	checkpoint.Name = "Stage"
	
	local spawnpoint = Instance.new("IntValue", hidden)
	spawnpoint.Name = "SpawnPoint"
	
	if GetSave then
		checkpoint.Value = GetSave
		print("Data Loaded For " .. player.Name)
	else
		checkpoint.Value = 1
		print("New Data Created For " .. player.Name)
	end
	
	local character = player.CharacterAdded:wait()
	local respawn = cpFolder[player.leaderstats.Stage.Value]
	character.HumanoidRootPart.CFrame = respawn.CFrame + Vector3.new(0,3,0)
	
	player.CharacterAdded:Connect(function(character)
		repeat wait() until workspace:FindFirstChild(character.Name)

		local player = game.Players:GetPlayerFromCharacter(character)
		local respawn = cpFolder[player.hidden.SpawnPoint.Value]

		character.HumanoidRootPart.CFrame = respawn.CFrame + Vector3.new(0,3,0)
	end)

end)

what happens when you run it now? I just copy/pasted it and there are no errors in the script

No errors, I’m trying to make it so they teleport to their current stage and it didn’t work. Maybe because I have a spawn point?

game.Players.PlayerAdded:Connect(function(player)
local key = “player-” … player.UserId

local GetSave = DataStore:GetAsync(key) – check for existing data

local leaderstats = Instance.new(“Folder”, player)
leaderstats.Name = “leaderstats”

local hidden = Instance.new(“Folder”, player)
hidden.Name = “hidden”

local checkpoint = Instance.new(“IntValue”, leaderstats)
checkpoint.Name = “Stage”

local spawnpoint = Instance.new(“IntValue”, hidden)
spawnpoint.Name = “SpawnPoint”

local character = player.CharacterAdded:wait()
local respawn

if GetSave then
checkpoint.Value = GetSave
print("Data Loaded For " … player.Name)
respawn = cpFolder[player.leaderstats.Stage.Value] – Saved Spawnpoint
else
checkpoint.Value = 1
print("New Data Created For " … player.Name)
respawn = cpFolder[player.hidden.SpawnPoint.Value] – Default Spawnpoint
end

character.HumanoidRootPart.CFrame = respawn.CFrame + Vector3.new(0,3,0)

end)
try this

Nope, that just broke the entire game in terms of data. I’d recommend not touching the bottom part of the script. No errors.

You don’t need any of this code at the bottom:

player.CharacterAdded:Connect(function(character)
repeat wait() until workspace:FindFirstChild(character.Name)

  local player = game.Players:GetPlayerFromCharacter(character)
  local respawn = cpFolder[player.hidden.SpawnPoint.Value]

  character.HumanoidRootPart.CFrame = respawn.CFrame + Vector3.new(0,3,0)

end)

because you’ve already called the CharacterAdded function above it:

local character = player.CharacterAdded:wait()

and they both set a different spawn location which is where I think it’s messing up for you.
Thats why I would recommend setting the spawn location of the player in the 'if GetSave then ’ part of the code

1 Like

I do need that code at the bottom, it’s a hidden leaderstat value which teleports you to a stage if you’ve touched a previous checkpoint. So, yes I do in fact need it.

(bump) still having this issue.

Didn’t work, got this error:
“[18:15:30.091 - ServerScriptService.Library.Datastore:36: attempt to index nil with ‘Character’]”

Script:

game.Players.PlayerAdded:Connect(function(player)
	local key = "player-" .. player.UserId

	local GetSave = DataStore:GetAsync(key) -- check for existing data 

	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"

	local hidden = Instance.new("Folder", player)
	hidden.Name = "hidden"

	local checkpoint = Instance.new("IntValue", leaderstats)
	checkpoint.Name = "Stage"

	local spawnpoint = Instance.new("IntValue", hidden)
	spawnpoint.Name = "SpawnPoint"

	if GetSave then
		checkpoint.Value = GetSave
		print("Data Loaded For " .. player.Name)
	else
		checkpoint.Value = 1
		print("New Data Created For " .. player.Name)
	end
	
	local LocalPlayer = game:GetService("Players").LocalPlayer
	local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
	local respawn2 = cpFolder[player.leaderstats.Stage.Value]
	Character.HumanoidRootPart.CFrame = respawn2.CFrame + Vector3.new(0,3,0)

	player.CharacterAdded:Connect(function(character)
		repeat wait() until workspace:FindFirstChild(character.Name)

		local player = game.Players:GetPlayerFromCharacter(character)
		local respawn = cpFolder[player.hidden.SpawnPoint.Value]

		character.HumanoidRootPart.CFrame = respawn.CFrame + Vector3.new(0,3,0)
	end)

end)

Your getting that error because you are trying to access the local player on a server script again:

local LocalPlayer = game:GetService(“Players”).LocalPlayer
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()

You already have the player object as it was passed to the function at the start:

game.Players.PlayerAdded:Connect(function(player)

local LocalPlayer = game:GetService(“Players”).LocalPlayer << Delete this line as it’s not needed
local Character = player.Character or player.CharacterAdded:Wait()

Maybe you have a valid reason, but I don’t get why you need to spawn the player twice, first they spawn to local respawn2 = cpFolder[player.leaderstats.Stage.Value] and then pretty much straight away they are spawned to local respawn = cpFolder[player.hidden.SpawnPoint.Value]

1 Like