local dss = game:GetService("DataStoreService")
local ds1 = dss:GetDataStore("rap")
local ds2 = dss:GetDataStore("inv")
game.Players.PlayerAdded:Connect(function(plr)
local stats = Instance.new("Folder",plr)
stats.Name = "leaderstats"
local inv = Instance.new("Folder",game.ReplicatedStorage.Inventories)
inv.Name = plr.Name.."Inventory"
local rap = Instance.new("IntValue",stats)
rap.Name = "RAP"
rap.Value = ds1:GetAsync(plr.UserId) or 0
end)
game.Players.PlayerRemoving:Connect(function(plr)
local stats = plr:FindFirstChild("leaderstats")
local inv = plr:FindFirstChild(plr.Name.."Inventory")
if stats and inv then
pcall(function()
ds1:SetAsync(plr.UserId,stats.RAP.Value)
end)
end
end)
game:BindToClose(function()
for _,plr in pairs(game.Players:GetChildren()) do
local stats = plr.leaderstats
local RAP = stats.RAP
pcall(function()
ds1:SetAsync(plr.UserId,RAP.Value)
end)
end
end)
For some reason it doesn’t save when I rejoin, and I used an answer to the problem from a previous post, and it worked the time then, but now it doesn’t work. The post was only made 11 days ago.
Ah, i see, it is because player leaves too early, script cant get time to read what player had, try making player data in one table and make client send that data upon leaving game
Okay, I fixed it. Yet I did mention there was no plugins installed on the account being used for this, and no models. But it doesn’t work in-game either.
local DataStore = game:GetService("DataStoreService"):GetDataStore("TestDataStore")
game.Players.PlayerAdded:Connect(function(player)
local folder = Instance.new("Folder")
folder.Name = "leaderstats"
folder.Parent = player
local currency = Instance.new("IntValue")
currency.Name = currencyName
currency.Parent = folder
local ID = currencyName.."-"..player.UserId
local savedData = nil
pcall(function()
savedData = DataStore:GetAsync(ID)
end)
if savedData ~= nil then
currency.Value = savedData
print("Data loaded")
else
currency.Value = 0
print("New player")
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local ID = currencyName.."-"..player.UserId
DataStore:SetAsync(ID,player.leaderstats[currencyName].Value)
end)
game:BindToClose(function()
for i, player in pairs(game.Players:GetPlayers()) do
if player then
player:Kick("Error")
end
end
wait(5)
end)```