Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

How do I make leaderstats that save when you leave the game?

Asked by 5 years ago

Saving and loading leaderboard

I know how to create a leaderboard but I do not know how to save it when a player leaves the game and load when the player rejoins. I am trying to do tutorials but all five of them did not work for me. All I ask is that someone who knows this stuff gives me an answer on how to make a saving and loading leaderboard

my code is down here and this is the tutorial I used for it: https://www.youtube.com/watch?v=lS20iM7iRQE

local datastore = game:GetService("DataStoreService") local ds1 = datastore:GetDataStore("GemSaveSystem") local ds2 = datastore:GetDataStore("CashSaveSystem") game.Players.PlayerAdded:connect(function(plr) local folder = Instance.new("Folder", plr) folder.Name = "leaderstats" local gems = Instance.new("IntValue", folder) gems.Name = "Gems" local cash = Instance.new("IntValue", folder) cash.Name = "Cash" gems.Value = ds1:GetAsync(plr.UserId) or 0 ds1:SetAsync(plr.UserId, gems.Value) cash.Value = ds2:GetAsync(plr.UserId) or 0 ds2:SetAsync(plr.UserId, cash.Value) gems.Changed:connect(function() ds1:SetAsync(plr.UserId, gems.Value) end) cash.Changed:connect(function() ds2:SetAsync(plr.UserId, cash.Value) end) end)
0
Are you playing your game in Studio or on Player? If you're doing it in Studio, you have to activate the DataStoreService in game settings. BlueGrovyle 278 — 5y
0
that indentation is killin me The_Pr0fessor 595 — 5y
0
Lmao HomieFirePGN 137 — 5y

2 answers

Log in to vote
2
Answered by 5 years ago
Edited 5 years ago

here is more info about DataStore: https://developer.roblox.com/articles/Data-store

Take Note:

  • Turn on Enable Studio Access To API Services
  • Should be a script
  • Put in ServerScriptService
  • Make your game a test when you turn ^^ on.
  • Copy the test to a new game.
  • Turn off Access To API Services once copied.

Accept my answer if it works for you!

Edited Script:

local dataStoreService = game:GetService("DataStoreService") --Service for DataStore
local cashDataStore = dataStoreService:GetDataStore("cashDataStore")--DataStore for Cash

game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder",player)
    leaderstats.Name = "leaderstats"

    local cash = Instance.new("IntValue",leaderstats)
    cash.Name = "Cash"

    local data -- keep nil for now
    local success,errormessage = pcall(function() --Message for success or error
        data = cashDataStore:GetAsync(player.UserId.."-Cash") --Put "-Cash" in case you want to add more values and to know what value
    end)
    if success then --Checks if data is successfully loaded
        cash.Value = data
    else
        print("There was an error while getting data")
        warn(errormessage) --Warn is like print, but in yellow
    end
end)
game.Players.PlayerRemoving:Connect(function(player) -- When player is removed do
    local success,errormessage = pcall(function()
        cashDataStore:SetAsync(player.UserId.."-Cash",player.leaderstats.Cash.Value)
    end)
    if success then
        print("Data successfully saved!")
    else
        print("There was an error when saving data!")
        warn(errormessage)
    end
end)

Hi, this is the script. I hope you don't just copy and paste. Just to make it easier for you to understand, I added some comments. I understand that you're a beginner so just to let you under stand more, here are some more links. Read up! Click me! Click me! Click me! Click me! Click me!

0
Many edits ikr HomieFirePGN 137 — 5y
0
I am sorry. I am a noob at scripting and even with that i could not figure it out. Can i ask for a script with one save thing that saves leaderstats called "Cash". I know I will learn from it by studying the code. purplemaniak7 10 — 5y
0
Did mine not work? spot6003 64 — 5y
0
Sure, ill get back to you HomieFirePGN 137 — 5y
View all comments (12 more)
0
Wait, cash or gems HomieFirePGN 137 — 5y
0
Just cash and none of them worked. Probably because I am the only one able to mess this up purplemaniak7 10 — 5y
0
There you go. HomieFirePGN 137 — 5y
0
I tried your script first copy-pasting promising if it worked I would look it over and see what was done to complete this. But it did not work. I tried to edit the script with the very little scripting i knew but then it just stopped working completely. So I tried the links you put there. I tried 3 of them before this but none of them helped me. purplemaniak7 10 — 5y
0
Put in the the serverscript service and it must not be a local script HomieFirePGN 137 — 4y
0
Sorry for late reply though HomieFirePGN 137 — 4y
0
thx, i will try that purplemaniak7 10 — 4y
0
did it work? HomieFirePGN 137 — 4y
0
Hello, its been one month and I haven't heard from you ever since. Just want to know if you fixed the problem or not. Thanks. HomieFirePGN 137 — 4y
0
sorry it has been a while purplemaniak7 10 — 4y
0
I am now working on a tycoon game and I am a bit better at scripting so im thinking i can insert numValues in the player via script and each of them will tell if the player has the droppers or not but i still cannot save them, by looking at the script, it looks like it would create another set of leaderstats so i could cut some parts off but the problem is i do not know where to cut it off... purplemaniak7 10 — 4y
0
...and i am using zednov's kit so i cant rally delete the leaderboard without breaking multiple things purplemaniak7 10 — 4y
Ad
Log in to vote
-1
Answered by 5 years ago
Edited 5 years ago

What I would suggest is adding on a pcall that ensures that if data fails to save or load that you will know in the developer log and you could also let the player know as well. This is the code I use to save my player data, i could of made it more efficient with the parenting yes, but you get the idea. This should work, try it out. Hope this helps!

    local DataStoreService = game:GetService("DataStoreService")

local OrbDataStore = DataStoreService:GetDataStore("OrbDataStore")

local TicketDataStore = DataStoreService:GetDataStore("TicketDataStore")



game.Players.PlayerAdded:Connect(function(player)

local leaderstats = Instance.new("Folder")

leaderstats.Name = "leaderstats"

leaderstats.Parent = player

local orbs = Instance.new("IntValue")

orbs.Name = "Orbs"

orbs.Parent = leaderstats

local tickets = Instance.new("IntValue")

tickets.Name = "Tickets"

tickets.Parent = leaderstats

local data

local success, errormessage = pcall(function()

data = OrbDataStore:GetAsync(player.UserId.."orbs")

end)

if success then

orbs.Value = data

else

print("There was an error while getting your Orb data")

warn(errormessage)

end

local success, errormessage = pcall(function()

data = TicketDataStore:GetAsync(player.UserId.."tickets")

end)

if success then

tickets.Value = data

else

print("There was an error while grabbing your Ticket data")

warn(errormessage)

end

end)



game.Players.PlayerRemoving:Connect(function(player)

local success, errormessage = pcall(function()

OrbDataStore:SetAsync(player.UserId.."orbs",player.leaderstats.Orbs.Value)

end)

if success then

print("Orb Data Successfully Saved")

else

print("There was an error while saving Orb data")

warn(errormessage)

end

local success, errormessage = pcall(function()

TicketDataStore:SetAsync(player.UserId.."tickets",player.leaderstats.Tickets.Value)

end)

if success then

print("Ticket Data Successfully Saved")

else

print("There was an error while saving Ticket data")

warn(errormessage)

end

end)

Answer this question