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

datastore could be Tricky?

Asked by
gloveshun 119
4 years ago
Edited 4 years ago

my made rebirth system not want to save, idk why, i know how to store data, but i think i be bad programmer

--This saves points but not rebirths (points(1)rebirths(2))
local DS = game:GetService("DataStoreService"):GetDataStore("SaveNeedData")
game.Players.PlayerAdded:Connect(function(plr)
    wait()
    local playerCall = "id_"..plr.userId
    local points = plr.Stats.Pyramids
    local rebirths = plr.Stats.Prestiges

    local GetSaved = DS:GetAsync(playerCall)
    if GetSaved then
        points.Value = GetSaved[1]
        rebirths.Value = GetSaved[2]

    else
        local NumbersForSaving = {points.Value, rebirths.Value}
        DS:GetAsync(playerCall, NumbersForSaving)
    end
    while wait() do
        DS:SetAsync("id_"..plr.userId, {plr.Stats[2].Value, plr.Stats[1].Value})
    end
end)
0
You're not a bad programmer! Datastore can definitely be tricky. I encourage you to post your code here so we can see what needs to be fixed and explain what we changed. royaltoe 5144 — 4y
0
k then gloveshun 119 — 4y

1 answer

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

It can start tricky but when you learn it it seems so easy. litteraly all you need to worry about is setting and getting.

game:GetService("DataStoreService")--call the service
game:GetService("DataStoreService"):GetDataStore("name of datastore")--call the name (if it doesnt have any value like if oyu just made it it automatically creats it, so dont worry about that, you can make s script when any name and just test and it will find it.
game:GetService("DataStoreService"):GetDataStore("name of datastore"):SetAsync()--set
game:GetService("DataStoreService"):GetDataStore("name of datastore"):GetAsync()--get
--how to use set and get:--
game:GetService("DataStoreService"):GetDataStore("name of datastore"):SetAsync(Key,Value)
game:GetService("DataStoreService"):GetDataStore("name of datastore"):GetAsync(Key)
--Examples:--
game:GetService("DataStoreService"):GetDataStore("name of datastore"):SetAsync("Row1","true")
if game:GetService("DataStoreService"):GetDataStore("name of datastore"):GetAsync("Row1") == "true" then
print hi--it then will print it because before i set the key to true in |-name of datastore-| datastore

easy set and get. this is example script for when you press button it toggles(switches between two) two values.

game:GetService("DataStoreService"):GetDataStore("Toggle"):SetAsync("trueORfalse","true")--to start when game starts

script.Parent.MouseButton1Click:Connect(function()
if game:GetService("DataStoreService"):GetDataStore("Toggle"):GetAsync("trueORfalse") == "true" then
game:GetService("DataStoreService"):GetDataStore("Toggle"):SetAsync("trueORfalse","false")
elseif game:GetService("DataStoreService"):GetDataStore("Toggle"):GetAsync("trueORfalse") == "false then"
game:GetService("DataStoreService"):GetDataStore("Toggle"):SetAsync("trueORfalse","true")
end
end)

and keeps going on forever. So now to answer your question.

DS = game:GetService("DataStoreService"):GetDataStore("SaveNeedData")--setting a value
--just saying make sure all spelling is correct

game.Players.PlayerAdded:Connect(function(plr)--actual code
    wait()
----------------------------------------------------
    points = plr.Stats.Pyramids
    rebirths = plr.Stats.Prestiges
---------------------------------------
    if DS:GetAsync(plr.userId) == nil then
        points.Value = DS:GetAsync(plr.userId)[1]--i dont even know what these number are for but fine
        rebirths.Value = DS:GetAsync(plr.userId)[2]

    else
        NumbersForSaving = {points.Value, rebirths.Value}
        DS:SetAsync(plr.userid, NumbersForSaving)
    end
    while wait() do
        DS:SetAsync(plr.userId, {plr.Stats[2].Value, plr.Stats[1].Value})
    end
end)

Changes Are:

Dont Put Local, its just a pain. theres no need to anyway, its code that only works for a block of code

Always press enter(put empty line) between setting values and the actual code

You use too many values, only set important one with not enough room to fit on whole line, like a=12, that is short, that does not need to be a set value, just say blahblah(12)

You dont even need the "id_".., just put plr.userid, but its probably too late because you most likely already created this datastore, just dont put extra stuff in for the future. thats why i directly put it userid

i changed get async to set because there were two variables there for get, get can only be one, get is for checking so after it you put == something.

and you cant put two variables in one variable, i saw that- you made NumberForSaving = (variable1, variable2), and you made that into ONE variable which is NumberForSaving and put it in SetASync(plr.userid,NmbersforSaving),so basically, thats saying this: SetAsync(38726,pointsvalue,rebirtsvalue)THATS THREE VARIABLES. thats why it saves one, it has to store that second variable, so the way it does it is by setting that datastore using the FIRST ONE, which is points.Value. instead of this change it one more time to make two parts, rebirth and points. wo make that script this:

 if DS:GetAsync(plr.userId) == nil then
        points.Value = DS:GetAsync(plr.userId)[1]--i dont even know what these number are for but fine
        rebirths.Value = DS:GetAsync(plr.userId)[2]

    else
        --numbers for saving deleted because we dont need it
        DS:SetAsync(plr.userid.."points", points.Value)
DS:SetAsync(plr.userid.."rebirths", rebirths.Value)
    end
   --[[ while wait() do
        DS:SetAsync(plr.userId, {plr.Stats[2].Value, plr.Stats[1].Value})
    end]]--i dont know what all this is so i just ignored it by making it green and not real code.
end)

but do whatever you want

0
Thanks gloveshun 119 — 4y
Ad

Answer this question