Hiya guys! Welcome to another episode of “struggling with simple concepts”!!!
Jokes aside, I have a obby that uses leader stats to save. Now everything works great, when a player dies they respawn at their checkpoint and stuff, and the game saves their value. Now your thinking, well, it all seems right, but no, it’s not!
When the player leaves it saves their value, but when they join it doesn’t spawn the player in their value’s spawn point. For example, if I leave with my leaderstat value equalling 2, it will still spawn me at respawn pad 1, but the value will still have saved.
Character loading script, this is what makes the player spawn at pads related to leaderstats:
local checkpoints = workspace:WaitForChild("checkpoints")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local stage = Instance.new("IntValue")
stage.Name = "Stage"
stage.Value = 1
stage.Parent = leaderstats
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)
This is what saves the value:
local DSS = game:GetService("DataStoreService")
local DataStore = DSS:GetDataStore("Stage")
game.Players.PlayerAdded:Connect(function(player)
local data
local success, errorMessage = pcall(function()
data= DataStore:GetAsync(player.UserId)
end)
if success then
player.leaderstats.Stage.Value = data
else
warn(errorMessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local data
local success, errorMessage = pcall(function()
local data=DataStore:SetAsync(player.UserId, player.leaderstats.Stage.Value)
end)
if success then
print("Data saved")
else
warn(errorMessage)
end
end)
game:BindToClose(function()
for i, v in pairs(game.Players:GetPlayers()) do
v:Kick()
print("Server shut down")
end
wait(2)
end)
No matter what the player’s value is, it only spawns at the first pad.