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

How to data save these stats?

Asked by 5 years ago
1local DataStore = game:GetService("DataStoreService")
2local ds = DataStore:GetDataStore("CashSaveSystem")
3 
4game.Players.PlayerRemoving:connect(function(player)
5 ds:SetAsync(player.UserId, player.leaderstats.Points.Value)
6end)

that is the code i need to data store but i cant find any way to, i cant really make a new script because i have a game based on this script and making a new one would end up just destroying my game

0
Is that the entire script or have you cut out most of it? MrCatDoggo 213 — 5y
0
That is all User#30123 0 — 5y
0
Well are you trying to get the stats back after saving or do you want to save more than just the points? That saves and works easily MrCatDoggo 213 — 5y
0
i want to get the stats back after saving User#30123 0 — 5y

4 answers

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

AS you want to get the stats back after saving you would be using the GetAsync part of datastores. As its just points the script is easy.

Add this bit of code after your Points.Value = 0

1local PlayerPoints = ds:GetAsync(player.UserId)
2 
3if PlayerPoints ~= nil then
4    Points.Value = PlayerPoints
5end

Its that simple

0
what do you mean b changing player.userid to a key what is the key for? User#30123 0 — 5y
0
Because you didnt show the script that makes the leaderstats i dont know what that script defines the player as which is why i used player.UserId. That is the key for the saves of that player. It can literally be anything like i mostly use "Plr_" .. Plr.UserId. Changing the key will change the saves for each player. Same as changing the datastore name MrCatDoggo 213 — 5y
0
You can literally just edit your question MrCatDoggo 213 — 5y
0
where did you edit? User#30123 0 — 5y
View all comments (3 more)
0
just a bit of text above the code MrCatDoggo 213 — 5y
0
its underlining with a blue line under ds and not working User#30123 0 — 5y
0
because you need to include the same locals you have for the save script MrCatDoggo 213 — 5y
Ad
Log in to vote
0
Answered by
Gingerely 245 Moderation Voter
5 years ago

So first of all you need to turn on studio api services. So instead of using that script, I would recommend using this. So insert a script in ServerScriptService and name it whatever you want, copy and paste this code in it. Every time you add a new stat then, basically make another line under savevalue = plr.leaderstats.Points and type in something like savevalue2 = plr.leaderstats.Cash and dont forget to do savevalue2.Value and at very bottom put a comma after the leaderstats.Points.Value and add plr.leaderstats.Cash.Value. Hope this was helpful.

local DS = game:GetService("DataStoreService"):GetDataStore("Datae") game.Players.PlayerAdded:Connect(function(plr) wait() local plrkey = "id_"..plr.userId local savevalue = plr.leaderstats.Points

1local GetSaved = DS:GetAsync(plrkey)
2if GetSaved then
3    savevalue.Value = GetSaved[1]
4 
5else
6    local NumbersForSaving = {savevalue.Value}
7    DS:GetAsync(plrkey, NumbersForSaving)
8end

end)

game.Players.PlayerRemoving:Connect(function(plr) DS:SetAsync("id_"..plr.userId, {plr.leaderstats.Points.Value}) end)

0
local DS = game:GetService("DataStoreService"):GetDataStore("Datae") game.Players.PlayerAdded:Connect(function(plr) wait() local plrkey = "id_"..plr.userId local savevalue = plr.leaderstats.Points view source 1 local GetSaved = DS:GetAsync(plrkey) 2 if GetSaved then 3 savevalue.Value = GetSaved[1] 4 5 else 6 local NumbersForSaving = {savevalue.Value} 7 DS:GetAsync(plrkey, NumbersFo Gingerely 245 — 5y
0
You dont need studio api services to test the game.You can do that by going into the game and running just some certain code on the dev consle to change your points, leave then rejoin MrCatDoggo 213 — 5y
0
what is the key for? User#30123 0 — 5y
0
MrCatDoggo, that is not what i mean. Data store is reached using api services. Therefore to save stats you need to have api services on. Gingerely 245 — 5y
View all comments (11 more)
0
Only if your testing the saving in studio there Gingerely MrCatDoggo 213 — 5y
0
Robotcake plrkey there is a local variable. Used for shortcuts. So player does not have to press anything to save dont worry. Gingerely 245 — 5y
0
that is true MrCatDoggo, however you have to make sure you did everything right. Gingerely 245 — 5y
0
One advantage of studio API service access is you can edit a players stats directly through the studio command bar. MrCatDoggo 213 — 5y
0
Oh, that I did not know haha. Thank you Gingerely 245 — 5y
0
i did that and pasted the code right after that script it did nothing i have api on User#30123 0 — 5y
0
not only this Gingerely 245 — 5y
0
Ye all you need is they key you use to save players stats, how you save said stats (In a table if multiple stats are being saved at once) and the players id (If the game uses the playerid as the key (Which most do but ive seen some use playername instead. MrCatDoggo 213 — 5y
0
local GetSaved = DS:GetAsync(plrkey) 2 if GetSaved then 3 savevalue.Value = GetSaved[1] 4 5 else 6 local NumbersForSaving = {savevalue.Value} 7 DS:GetAsync(plrkey, NumbersForSaving) 8 end this in the script lines are only one part of the script, others are not in the line. If u look on top of the box and to the bottom there are still more codes. Gingerely 245 — 5y
0
MrCatDoggo, could you try to answer my question? It is the second one in front page. Gingerely 245 — 5y
0
I can give it a go MrCatDoggo 213 — 5y
Log in to vote
0
Answered by 5 years ago
01BadGold = script.Parent
02isHidden = false
03 
04--Give the player a Points leaderboard when they join the game
05game.Players.PlayerAdded:Connect(function(player)
06    local leaderstats = Instance.new("Folder")
07    leaderstats.Name = "leaderstats"
08    leaderstats.Parent = player
09 
10    local Points = Instance.new("IntValue")
11    Points.Parent = leaderstats
12    Points.Name = "Points"
13    Points.Value = 0
14end)
15 
View all 35 lines...

the rest of the code is for when i touch a part i get a point. i thought it will help you to know

0
Check my edited answer MrCatDoggo 213 — 5y
0
it didnt change User#30123 0 — 5y
Log in to vote
0
Answered by 5 years ago

you set the data. but don't load it in when the player joins the game. and i recommend to put it in a pcall() so it won't stop the whole script if it errors.

01local DataStore = game:GetService("DataStoreService")
02local ds = DataStore:GetDataStore("CashSaveSystem")
03 
04game.Players.PlayerAdded:connect(function(player)
05    pcall(function()
06        local player.leaderstats.Points.Value = ds:GetAsync(player.UserId, player.leaderstats.Points.Value) or 0
07    end
08end
09 
10game.Players.PlayerRemoving:connect(function(player)
11    pcall(function()
12        ds:SetAsync(player.UserId, player.leaderstats.Points.Value)
13    end
14end)

the reason why i put "or 0" is because if the player joins and they don't have any data then we would need to change it to 0. hope this helped!

Answer this question