Hi im trying to save two Values when a player leaves the game.
Currently it saves the amount called “Pounds” and this works fine!
local Datastoreservice = game:GetService("DataStoreService")
local ds = Datastoreservice:GetDataStore("PlayerData")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Pounds = Instance.new("IntValue")
Pounds.Name = "Pounds"
Pounds.Parent = leaderstats
local Level = Instance.new("IntValue")
Level.Name = "Level"
Level.Parent = leaderstats
Level.Value = 1
local data = ds:GetAsync(player.UserId)
if data then
Pounds.Value = data
else
Pounds.Value = 0
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local id = player.UserId
local success, err = pcall(function()
ds:SetAsync(id, player.leaderstats["Pounds"].Value)
end)
end)
game:BindToClose(function()
for i, player in pairs(game.Players:GetChildren()) do
local id = player.UserId
local success, err = pcall(function()
ds:SetAsync(id, player.leaderstats.Pounds.Value)
end)
end
end)
Heres the script showing how it saves Pounds.
I would like to also save the Value “Level”
I have tried adding "ds:SetAsync(id, player.leaderstats[“Level”].Value) "
But that didnt work.
Could anyone help me please? Thankyou!!
local Datastoreservice = game:GetService(“DataStoreService”)
local dsPounds = Datastoreservice:GetDataStore(“PlayerData”)
local dsLevel = Datastoreservice:GetDataStore(“PlayerData”)
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new(“Folder”)
leaderstats.Name = “leaderstats”
leaderstats.Parent = player
local Pounds = Instance.new(“IntValue”)
Pounds.Name = “Pounds”
Pounds.Parent = leaderstats
local dss = game:GetService(“DataStoreService”)
local ds = dss:GetDataStore(“PoundsData”)
local ds1 = dss:GetDataStore(“LevelData”)
function saveData(plr)
local pounds = plr.leaderstats.Pounds.Value
local level = plr.leaderstats.Level.Value
ds:SetAsync(plr.UserId, pounds)
ds1:SetAsync(plr.UserId, level)
end
game.Players.PlayerRemoving:Connect(saveData)
game:BindToClose(function()
for i, plr in pairs(game.Players:GetPlayers()) do
saveData(plr)
end
end)
game.Players.PlayerAdded:Connect(function(plr)
local ls = Instance.new("Folder")
ls.Name = "leaderstats"
ls.Parent = plr
local poundsVal = Instance.new("IntValue")
poundsVal.Name = "Pounds"
poundsVal.Parent = ls
local levelVal = Instance.new("IntValue")
levelVal.Name = "Level"
levelVal.Parent = ls
--Saved DATA --
local poundsData = ds:GetAsync(plr.UserId) or 0
poundsVal.Value = poundsData
local levelData = ds1:GetAsync(plr.UserId) or 1
levelVal.Value = levelData