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
local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("CashSaveSystem")

game.Players.PlayerRemoving:connect(function(player)
 ds:SetAsync(player.UserId, player.leaderstats.Points.Value)
end)


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 Robotcake8 4 — 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 Robotcake8 4 — 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

local PlayerPoints = ds:GetAsync(player.UserId)

if PlayerPoints ~= nil then
    Points.Value = PlayerPoints
end

Its that simple

0
what do you mean b changing player.userid to a key what is the key for? Robotcake8 4 — 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? Robotcake8 4 — 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 Robotcake8 4 — 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

local GetSaved = DS:GetAsync(plrkey)
if GetSaved then
    savevalue.Value = GetSaved[1]

else
    local NumbersForSaving = {savevalue.Value}
    DS:GetAsync(plrkey, NumbersForSaving)
end

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? Robotcake8 4 — 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 Robotcake8 4 — 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
BadGold = script.Parent
isHidden = false 

--Give the player a Points leaderboard when they join the game
game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local Points = Instance.new("IntValue")
    Points.Parent = leaderstats
    Points.Name = "Points"
    Points.Value = 0
end)


--Fires a function when the player touches the BadGold
BadGold.Touched:Connect(function(hit)
    --if the BadGold is not hidden and the thing touching the BadGold is a player...
    if(not isHidden and game.Players:FindFirstChild(hit.Parent.Name))then
        --give the player one point and make the brick hidden again
        local player = game.Players[hit.Parent.Name]

        player.leaderstats.Points.Value = player.leaderstats.Points.Value + 1

        BadGold.Transparency = 1
        isHidden = true

        wait(2) -- wait 2 seconds to make the BadGold visible again after being touched

        isHidden = false 
        BadGold.Transparency = 0

    end
end)

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 Robotcake8 4 — 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.

local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("CashSaveSystem")

game.Players.PlayerAdded:connect(function(player)
    pcall(function()
        local player.leaderstats.Points.Value = ds:GetAsync(player.UserId, player.leaderstats.Points.Value) or 0
    end
end

game.Players.PlayerRemoving:connect(function(player)
    pcall(function()
        ds:SetAsync(player.UserId, player.leaderstats.Points.Value)
    end
end)

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