Hello! I am making a game and I am trying to save the leaderboard data.
But I don’t want this script to be in this script
So I want to put the datasave script to a new script
How can I do that?
Thanks for reading!
Hello! I am making a game and I am trying to save the leaderboard data.
But I don’t want this script to be in this script
So I want to put the datasave script to a new script
How can I do that?
Thanks for reading!
In another script, you would use the Players.PlayerRemoving
event. Once you have setup the event, you would put all of the data into a table. Then you would do DataStore:SetAsync(key, value)
where the key would be the key for the specific player, and value would be the table.
first of all make a new script
Then make a dataStore variable
local dataStore = game:GetService(“DataStoreService”):GetDataStore(“name it whatever you want”)
Then add a playeradded event and make variables for the things you want to save
local whatever_it_is = player:WaitForChild(“leaderstats”).Whatever_it_is (do it for all the things you want to save)
then make a local cashData, rebirthsData
then you need to make a pcall function because sometimes dataStores can go down and if they do then we don’t want to lose everyone’s data, only that person
so do local success, errormessage = pcall(function()
cashData = DataStore:GetAsync(“cash-”…player.UserId)
rebirthsData = DataStore:GetAsync(“rebirths-”…player.UserId)
end)
if success then
if cashData then
cash.Value = cashData
end
if rebirthsData then
rebirths.Value = rebirthsData
end
end
Then outside the playeradded event add a playerRemoving event
inside it say
local success,errormessage = pcall(function()
dataStore:SetAsync(“cash-”…player.UserId,player.leaderstats.Cash.Value)
dataStore:SetAsync(“rebirths-”…player.UserId,player.leaderstats.Rebirths.Value)
end)
please tell me if there are any mistakes and it doesn’t work
I also tried this script :
local ds = game:GetService(“DataStoreService”):GetDataStore(“SaveData”)
game.Players.PlayerAdded:Connect(function(plr)
wait()
local lss = “id_”…plr.userId
local Save1 = plr.leaderstats.Knowledge
local Save2 = plr.leaderstats.Rebirths
local Save3 = plr.leaderstats.Cash
local GetSaved = ds:GetAsync(lss)
if GetSaved then
Save1.Value = GetSaved[1]
Save2.Value = GetSaved[2]
Save3.Value = GetSaved[3]
else
local NumberForSaving = {Save1.Value, Save2.Value, Save3.Value}
ds:GetAsync(lss, NumberForSaving)
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
ds:SetAsync(“id_”…plr.userId, {plr.leaderstats.Knowledge.Value, plr.leaderstats.Rebirths.Value, plr.leaderstats.Cash.Value})
end)
But it is broken like when I join the game and gain some cash, etc , when I rejoin the game for example if I have 15 cash and rejoin my cash resets and now I have 15 rebirth somehow
but the other currency is saved successfully