Hello everyone, im making a obby with checkpoints but it seems that i have a bug, everytime a NEW player with no data joins they spawn mid air repeatedly falling to their death in the void. but when they rejoin they spawn correctly… anybody know i can make it so the first time it spawns them at the checkpoints?
here is my combined code for touching checkpoints and storing that data.
game.Players.PlayerAdded:Connect(function(player)
local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("stage")
local checkpoints = workspace.CheckPoints
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local stage = Instance.new("IntValue")
stage.Name = "stage"
stage.Parent = leaderstats
local stage = Instance.new("IntValue")
stage.Name = "Stage"
stage.Value = 1
stage.Parent = leaderstats
local data
local success, errormessage = pcall(function()
data = dataStore:GetAsync(player.UserId.."Data") --- "-your_key_name" can be changed anything eg "Data"
end)
if success then
stage.Value = data.Currency
stage.Value = data.Stage
else
print("There was an error.")
warn(errormessage)
end
player.CharacterAdded:Connect(function(char)
local hum = char:WaitForChild("Humanoid")
wait()
char:MoveTo(checkpoints[stage.Value].Position)
hum.Touched:Connect(function(hit)
if hit.Parent == checkpoints then
if tonumber(hit.Name) == stage.Value + 1 then
stage.Value = stage.Value + 1
end
end
end)
end)
end)
game.Players.PlayerRemoving:Connect(function(plr)
local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("stage")
local SaveData={Currency=plr.leaderstats.stage.Value, Stage=plr.leaderstats.Stage.Value}
local success, errormessage = pcall(function()
dataStore:SetAsync(plr.UserId.."Data", SaveData)
end)
if success then
print("Successfully saved Player Data!")
else
print("There was an error while saving player data!")
warn(errormessage)
end
end)