You’re getting data from the play outside the scope of the variable ‘player’. (you’re getting data from the player outside the PlayerAdded function???)
This should work.
local DataStore = game:GetService("DataStoreService"):GetDataStore("Stage")
local defaultstage = 1
local playersLeft = 0
game.Players.PlayerAdded:Connect(function(player)
playersLeft = playersLeft + 1
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(character)
character.Humanoid.WalkSpeed = 16
end)
local player_data
pcall(function()
player_data = DataStore:GetAsync(player.UserId.."-Stage")
end)
if player_data ~= nil then
stage.Value = player_data
else
stage.Value = defaultstage
end
end)
local bindablEvent = Instance.new("BindableEvent")
game.Players.PlayerRemoving:Connect(function(player)
pcall(function()
DataStore:SetAsync(player.UserId.."-Stage",player.leaderstats.Stage.Value)
print("Saved DS for "..player.Name.." ")
end)
playersLeft = playersLeft - 1
bindablEvent:Fire()
end)
game:BindToClose(function()
while playersLeft > 0 do
bindablEvent.Event:Wait()
end
end)