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