There’s an issue with me teleporting to the correct stages when navigating through stages and when joining the game, thought it works perfectly fine in studio. I’ve tested multiple times to see if this was just one case, however it seems to be happening all the time. This is the following error I get on the server:
This is the script that is getting errored:
local DataService = game:GetService("DataStoreService")
local DataStore = DataService:GetDataStore("data_01")
local camera = workspace.CurrentCamera
local cpFolder = game.Workspace:WaitForChild("Checkpoints")
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.Character
local respawn2 = cpFolder:WaitForChild(player.leaderstats.Stage.Value)
Character:WaitForChild("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)
game.Players.PlayerRemoving:Connect(function(player)
local key = "player-" .. player.UserId
DataStore:SetAsync(key, player.leaderstats.Stage.Value)
print("Data Successfully Saved For " .. player.Name)
end)