local ds = game:GetService("DataStoreService"):GetDataStore("SaveData")
game.Players.PlayerAdded:Connect(function(plr)
wait()
local plrkey = "id_"..plr.UserId
local save1 = plr.leaderstats.Coins
local save2 = plr.Level1
local GetSaved = ds:GetAsync(plrkey)
if GetSaved then
save1.Value = GetSaved[1]
save2.Value = GetSaved[2]
else
local NumberForSaving = {save1.Value}
ds:GetAsync(plrkey, NumberForSaving)
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
ds:SetAsync("id_"..plr.UserId,{plr.leaderstats.Coins.Value, plr.Level1.Value})
end)
I fixed your script for you . Its best that you put it in ServerScriptService.
local Players = game:GetService("Players")
local TestService = game:GetService("TestService")
local RunService = game:GetService("RunService")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStoreValues") --Name the DataStore whatever you want
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new('Folder')
leaderstats.Name = 'leaderstats'
leaderstats.Parent = player
local Coins = Instance.new('IntValue')
Coins.Name = 'Coins'
Coins.Parent = leaderstats
Coins.Value = 0
local Level1 = Instance.new('IntValue')
Level1.Name = 'Level1'
Level1.Parent = leaderstats
Level1.Value = 0
local Level2 = Instance.new('IntValue')
Level2.Name = 'Level2'
Level2.Parent = leaderstats
Level2.Value = 0
local Level3 = Instance.new('IntValue')
Level3.Name = 'Level3'
Level3.Parent = leaderstats
Level3.Value = 0
local Level4 = Instance.new('IntValue')
Level4.Name = 'Level4'
Level4.Parent = leaderstats
Level4.Value = 0
local Level5 = Instance.new('IntValue')
Level5.Name = 'Level5'
Level5.Parent = leaderstats
Level5.Value = 0
local value1Data = Coins
local value2Data = Level1
local value3Data = Level2
local value4Data = Level3
local value5Data = Level4
local value6Data = Level5
local s, e = pcall(function()
value1Data = DataStore:GetAsync(player.UserId..'-Value1') or 0 --check if they have data, if not it'll be "0"
value2Data = DataStore:GetAsync(player.UserId..'-Value2') or 0
value3Data = DataStore:GetAsync(player.UserId..'-Value3') or 0
value4Data = DataStore:GetAsync(player.UserId..'-Value4') or 0
value5Data = DataStore:GetAsync(player.UserId..'-Value5') or 0
value6Data = DataStore:GetAsync(player.UserId..'-Value6') or 0
end)
if s then
Coins.Value = value1Data
Level1.Value = value2Data
Level2.Value = value3Data
Level3.Value = value4Data
Level4.Value = value5Data
Level5.Value = value6Data
else
TestService:Error(e) --if not success then we error it to the console
end
end)
Players.PlayerRemoving:Connect(function(player)
local s, e = pcall(function()
DataStore:SetAsync(player.UserId..'-Value1', player.leaderstats.Coins.Value) --setting data
DataStore:SetAsync(player.UserId..'-Value2', player.leaderstats.Level1.Value)
DataStore:SetAsync(player.UserId..'-Value3', player.leaderstats.Level2.Value)
DataStore:SetAsync(player.UserId..'-Value4', player.leaderstats.Level3.Value)
DataStore:SetAsync(player.UserId..'-Value5', player.leaderstats.Level4.Value)
DataStore:SetAsync(player.UserId..'-Value6', player.leaderstats.Level5.Value)
end)
if not s then TestService:Error(e)
end
end)
game:BindToClose(function(player)
if not RunService:IsStudio() then
local s, e = pcall(function()
DataStore:SetAsync(player.UserId..'-Value1', player.leaderstats.Coins.Value) --setting data
DataStore:SetAsync(player.UserId..'-Value2', player.leaderstats.Level1.Value)
DataStore:SetAsync(player.UserId..'-Value3', player.leaderstats.Level2.Value)
DataStore:SetAsync(player.UserId..'-Value4', player.leaderstats.Level3.Value)
DataStore:SetAsync(player.UserId..'-Value5', player.leaderstats.Level4.Value)
DataStore:SetAsync(player.UserId..'-Value6', player.leaderstats.Level5.Value)
end)
if not s then TestService:Error(e)
end
end)
end)