Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local Datastore = game:GetService("DataStoreService")
- local playerSaveData = game.ServerStorage:WaitForChild('PlayerSaveData')
- game.Players.PlayerAdded:connect(function(player)
- print("PlayerAdded")
- local playerFolder = game.ReplicatedStorage.playerFolder:Clone()
- playerFolder.Name = player.Name
- playerFolder.Parent = game.ServerStorage.PlayerSaveData
- local c = Instance.new("BoolValue")
- c.Name = "CanAutoLoad"
- c.Parent = player
- c.Value = false
- local canSave = Instance.new("BoolValue")
- canSave.Name = 'canSave'
- canSave.Value = true
- canSave.Parent = player
- local key = "user " .. player.userId
- local playerData = {
- customCharacter = '';
- equipped = {};
- slots = {
- };
- stats = {
- food = nil;
- health = nil;
- melee = nil;
- movementSpeed = nil;
- stamina = nil;
- slotAmnt = nil;
- }
- }
- local dataFetchSuccess,ErrorMessage = pcall(function()
- playerData = Datastore:GetAsync(key)
- end)
- if dataFetchSuccess then --if success
- if playerData ~= nil then -- played before
- print("Player Has Played Before")
- playerFolder.CustomCharacter.SkinTone.Value = playerData.customCharacter
- playerFolder.Stats.Food.Value = playerData.stats.food
- playerFolder.Stats.Health.Value = playerData.stats.health
- playerFolder.Stats.Melee.Value = playerData.stats.melee
- playerFolder.Stats.MovementSpeed.Value = playerData.stats.movementSpeed
- playerFolder.Stats.Stamina.Value = playerData.stats.stamina
- playerFolder.Stats.SlotAmnt.Value = playerData.stats.slotAmnt
- for i = 1,#playerData.stats.slotAmnt do -- for each slots
- --Clone stat from storage to players slots folder and fill in data based on PlayerData
- print("Creating slot"..i)
- local slot = game:GetService("ReplicatedStorage").slot:Clone()
- slot.Name = "Slot"..i
- slot.ItemAmount.Value = playerData.slots["Slot"..i].slotName
- slot.ItemCap.Value = playerData.slots["Slot"..i].itemCap
- slot.SlotFull.Value = playerData.slots["Slot"..i].slotFull
- slot.Parent = playerFolder.Slots
- end
- else -- first time playing
- print("First Time Playing")
- playerFolder.CustomCharacter.SkinTone.Value = "Light Orange";
- playerFolder.Stats.Food.Value = 100;
- playerFolder.Stats.Health.Value = 100;
- playerFolder.Stats.Melee.Value = 1;
- playerFolder.Stats.MovementSpeed.Value = 1;
- playerFolder.Stats.Stamina.Value = 100;
- playerFolder.Stats.SlotAmnt.Value = 0;
- end
- else
- print("Couldn't retrieve data")
- canSave.Value = false
- player:Kick('Could not retrieve data.')
- end
- end)
- game.Players.PlayerRemoving:connect(function(Player)
- local Key = "user " .. Player.userId
- local b = game.ServerStorage.PlayerSaveData:FindFirstChild(Player.Name):GetChildren()
- if Player.canSave.Value==false then return end --Player data can't be saved, so do nothing.
- local PlayerData = {
- customCharacter = '';
- equipped = {};
- slots = {
- };
- stats = {
- food = game.ServerStorage.PlayerSaveData[Player.Name].Stats.Food.Value;
- health = game.ServerStorage.PlayerSaveData[Player.Name].Stats.Health.Value;
- melee = game.ServerStorage.PlayerSaveData[Player.Name].Stats.Melee.Value;
- movementSpeed = game.ServerStorage.PlayerSaveData[Player.Name].Stats.MovementSpeed.Value;
- stamina = game.ServerStorage.PlayerSaveData[Player.Name].Stats.Stamina.Value;
- slotAmnt = tonumber(#b);
- };
- }
- for i =1,#b do
- print("Saving")
- PlayerData.slots["Slot"..i] = {
- slotName = game.ServerStorage.PlayerSaveData[Player.Name].Slots:FindFirstChild("Slot"..i).Value;
- itemCap = game.ServerStorage.PlayerSaveData[Player.Name].Slots:FindFirstChild("Slot"..i).ItemCap.Value;
- slotFull = game.ServerStorage.PlayerSaveData[Player.Name].Slots:FindFirstChild("Slot"..i).SlotFull.Value;
- };
- end
- local DataWriteSuccess,ErrorMessage=pcall(function() --Once again, we are safely calling a web request. If it fails, we can safely handle it.
- Datastore:SetAsync(Key,PlayerData)
- end)
- if not DataWriteSuccess then
- print("Failed To Write")
- --Uh oh, player data didnt' save. Handle this error.
- --We will attempt to save the player's data 5 more times. If it fails after 5 times,
- --we will abort the save and the player's data will not be changed in the datastores.
- local Retry_Count=0
- while Retry_Count<6 do
- wait(60) --Wait 1 minute between each retry
- local Succeded,Error=pcall(function()
- Datastore:SetAsync(Key,PlayerData)
- end)
- if Succeded then break end --HURRAY, DATA SAVED!
- Retry_Count=Retry_Count+1
- end
- end
- end)
- game:BindToClose(function()
- for _, Player in pairs(game:GetService("Players"):GetPlayers()) do
- local Key = "user " .. Player.userId
- local b = game.ServerStorage.PlayerSaveData:FindFirstChild(Player.Name):GetChildren()
- if Player.canSave.Value==false then return end --Player data can't be saved, so do nothing.
- local PlayerData = {
- customCharacter = '';
- equipped = {};
- slots = {
- };
- stats = {
- food = game.ServerStorage.PlayerSaveData[Player.Name].Stats.Food.Value;
- health = game.ServerStorage.PlayerSaveData[Player.Name].Stats.Health.Value;
- melee = game.ServerStorage.PlayerSaveData[Player.Name].Stats.Melee.Value;
- movementSpeed = game.ServerStorage.PlayerSaveData[Player.Name].Stats.MovementSpeed.Value;
- stamina = game.ServerStorage.PlayerSaveData[Player.Name].Stats.Stamina.Value;
- slotAmnt = tonumber(#b);
- };
- }
- for i =1,#b do
- print("Saving")
- PlayerData.slots["Slot"..i] = {
- slotName = game.ServerStorage.PlayerSaveData[Player.Name].Slots:FindFirstChild("Slot"..i).Value;
- itemCap = game.ServerStorage.PlayerSaveData[Player.Name].Slots:FindFirstChild("Slot"..i).ItemCap.Value;
- slotFull = game.ServerStorage.PlayerSaveData[Player.Name].Slots:FindFirstChild("Slot"..i).SlotFull.Value;
- };
- end
- local DataWriteSuccess,ErrorMessage=pcall(function() --Once again, we are safely calling a web request. If it fails, we can safely handle it.
- Datastore:SetAsync(Key,PlayerData)
- end)
- if not DataWriteSuccess then
- print("Failed To Write")
- --Uh oh, player data didnt' save. Handle this error.
- --We will attempt to save the player's data 5 more times. If it fails after 5 times,
- --we will abort the save and the player's data will not be changed in the datastores.
- local Retry_Count=0
- while Retry_Count<6 do
- wait(60) --Wait 1 minute between each retry
- local Succeded,Error=pcall(function()
- Datastore:SetAsync(Key,PlayerData)
- end)
- if Succeded then break end --HURRAY, DATA SAVED!
- Retry_Count=Retry_Count+1
- end
- end
- end
- end)
Advertisement
Add Comment
Please, Sign In to add comment