my script will save the data but it wont load it when a player joins
local dataStoreService = game:GetService("DataStoreService")
local obbyDataStore = dataStoreService:GetDataStore("obbyDataStore")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local coins = Instance.new("IntValue",leaderstats)
coins.Name = "Coins"
coins.Value = 0
local stage = Instance.new("IntValue",leaderstats)
stage.Name = "Stage"
stage.Value = 0
local kills = Instance.new("IntValue",leaderstats)
kills.Name = "Kills"
kills.Value = 0
local datastagevar
local success, errormessage = pcall(function()
datastagevar = obbyDataStore:GetAsync(player.UserId,"-stagevar")
datastage = obbyDataStore:GetAsync(player.UserId,"-stage")
datakills = obbyDataStore:GetAsync(player.UserId,"-kills")
datacoins = obbyDataStore:GetAsync(player.UserId,"-coins")
end)
if success then
if datastagevar then
player.Team = game.Teams[datastagevar]
else
player.Team = game.Teams.Spawn
end
coins.Value = datacoins
kills.Value = datakills
stage.Value = datastage
print("data loaded")
end
end)
game:BindToClose(function() -- This code will run BEFORE the game closes, in other words the game cannot close until this code has ended
for _, player in pairs(game.Players:GetPlayers()) do -- loops through the players to get the player
local teamName = player.Team.Name
local success, errormessage = pcall(function()
obbyDataStore:SetAsync(player.UserId.."-stagevar",teamName)
obbyDataStore:SetAsync(player.UserId.."-stage",player.leaderstats.Stage.Value)
obbyDataStore:SetAsync(player.UserId.."-kills",player.leaderstats.Kills.Value)
obbyDataStore:SetAsync(player.UserId.."-coins",player.leaderstats.Coins.Value)
print("working")
end)
if success then
print ("Data Saved")
else
print("Error, i am now sentient human")
warn(errormessage)
end
end
end)
