so this doesnt work and i cant figure out why
any help is very appreciated
local DataStoreService=game:GetService("DataStoreService")
local DataStore=DataStoreService:GetDataStore("Player_Data")
--A player joined. Load/create their data.
game.Players.PlayerAdded:connect(function(Player)
local PlayerDataTable = {} --Will hold the player's data when fetched from the datastore.
local Stats=Instance.new('Folder')
Stats.Name="PlayersData"
Stats.Parent=Player
--All the variables that will be saved.
local Money=Instance.new('IntValue',Stats)
Money.Name="Money"
Money.Parent = Stats
local Example=Instance.new('IntValue',Stats)
Example.Name="Example"
Example.Parent = Stats
--Whether the player's data can be saved or not.
local CanSave = Instance.new('BoolValue',Player)
CanSave.Name = "CanSaveData"
CanSave.Value = true
--end of setting up value objects
local DataFetchSuccess,ErrorMessage = pcall(function() --pcalling it to prevent data loss
PlayerDataTable = DataStore:GetAsync(tostring(Player.UserId))
end)
if DataFetchSuccess then --The datastore GET request was successful!
--Loading or creating the player's data.
if PlayerDataTable~=nil then
Player.PlayersData.Money.Value = PlayerDataTable[1]
Player.PlayersData.Example.Value = PlayerDataTable[2]
print("Got "..Player.Name.."'s data!")
else
Player.PlayersData.Money.Value = 1000
Player.PlayersData.Example.Value = 5
print(Player.Name.." is new! Giving them the starter data!")
end
else --It wasn't able to get the player's data.
Player.CanSaveData.Value=false --Make sure that when they are kicked, it doesn't save nothing.
Player:Kick("Your data failed to load. Try rejoining.") --Make them rejoin the game.
end
end)
--When the player leaves the game, do the following.
game.Players.PlayerRemoving:connect(function(Player)
if Player.CanSaveData.Value==false then return end --Player data can't be saved, so do nothing and don't execute the following.
local PlayerDataTable = {
Money = Player.PlayersData.Money.Value,
Example = Player.PlayersData.Example.Value
}
local DataWriteSuccess,ErrorMessage=pcall(function() --Once again, we are safely calling a web request. If it fails, we can safely handle it.
DataStore:SetAsync(tostring(Player.UserId), PlayerDataTable)
end)
if not DataWriteSuccess then --The player's data didn't save! Retry!!! >:C
local Retry_Count=0
while Retry_Count<9 do
wait(5) --Wait between each retry.
local Succeded,Error=pcall(function()
DataStore:SetAsync(tostring(Player.UserId), PlayerDataTable)
end)
if Succeded then print(Player.Name.."'s data saved!") break end --Yay! The player's data saved!!! :DDDDDD
Retry_Count=Retry_Count+1
end
end
end)
game:BindToClose(function()
local players = game.Players:GetPlayers()
for _, Player in pairs(players) do
local PlayerDataTable = {
Money = Player.PlayersData.Money.Value,
Example = Player.PlayersData.Example.Value
}
local userId = Player.UserId
local data = PlayerDataTable[Player.UserId]
if data then
-- wrap in pcall to handle any errors
local success, result = pcall(function()
-- SetAsync yields so will stall shutdown
DataStore:SetAsync(tostring(Player.UserId), data)
end)
if not success then --The player's data didn't save! Retry!!! >:C
local Retry_Count=0
while Retry_Count<9 do
wait(10) --Wait between each retry.
local Succeded,Error=pcall(function()
DataStore:SetAsync(tostring(Player.UserId), PlayerDataTable)
end)
if Succeded then print(Player.Name.."'s data saved!") break end --Yay! The player's data saved!!! :DDDDDD
Retry_Count=Retry_Count+1
end
end
end
end
print("completed saving player data")
end)