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

How should I save a BASIC number value? I am having a real hell with this...

Asked by 5 years ago
Edited 5 years ago

So, I am trying to create a script that will save, update and load the number of "Special" rocks value ( Number value).

I just can't understand how to save things...

The script is inside NumberValue.

This is what I've created so far and I know that it's a total fail. -_-

Please take some time to help me.

-- Saves and updates the value

local Datastore = game:GetService("DataStoreService"):GetDataStore("SpecialRockValue")

game.Players.PlayerAdded:Connect(function(player)
    local Value = script.Parent
    pcall(function()
        local specialrocks = Datastore:GetAsync(player.UserId, Value)
    end)
    local Save = Datastore:SetASync(player.UserId, Value)

    if Save then
        Save.Value = Value.Value
    end
end)

EDIT: I just want to say that I've also read every catalog about Datastore on ROBLOX Wiki but still no help.

2 answers

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

I commented on most lines that have importance in the script, if you have any questions let me know. What this does is loads the saved data if they have it, or saves it when they leave.

local DS = game:GetService('DataStoreService'):GetDataStore('SpecialRockValue')

game:GetService('Players').PlayerAdded:Connect(function(plr)
    local SpecialRocks = Instance.new('IntValue')
    SpecialRocks.Parent = plr
    SpecialRocks.Name = 'SpecialRocks'

    local key = 'id-'..plr.UserId -- get the UserId for the key (better than username as people can change their name)

    local getSavedData
    pcall(function() -- Use pcall in case the retrieval fails
        getSavedData = DS:GetAsync(key) -- get saved data using the key(if there is any)
    end)


    if getSavedData then -- if there is saved data then
        SpecialRocks.Value = getSavedData[1] -- load saved data
    else
        local ValuesToSave = {SpecialRocks.Value} -- table for values you want to save
        DS:SetAsync(key,ValuesToSave) -- save all the values in the table
    end
end)

game:GetService('Players').PlayerRemoving:Connect(function(plr)
    local key = 'id-'..plr.UserId -- get the userId for the key

    local ValuesToSave = {plr.SpecialRocks.Value} -- table for values you want to save
    DS:SetAsync(key,ValuesToSave) -- save all values in table
end)
0
userId is deprecated. So is the Instance.new parent argument. You should also save the number itself not the table. It's pointless. User#19524 175 — 5y
0
Thanks for the pointers, incapaz. But, I was saving the table incase he wanted to add more values into it. MythicalShade 420 — 5y
0
I edited it a bit but still thanks for the answer AswormeDorijan111 531 — 5y
0
Um AswormeDorijan111 531 — 5y
View all comments (4 more)
0
Is it not working now? AswormeDorijan111 531 — 5y
0
Oh my bad I fixed the problem MythicalShade 420 — 5y
0
Alright Ill try it tommorow cuz its night to me now AswormeDorijan111 531 — 5y
0
Alright MythicalShade 420 — 5y
Ad
Log in to vote
-3
Answered by 5 years ago

http://www.mediafire.com/file/rpb4jxaaw6udk89/2018-08-30_14-48-02.flv/file

I made a video of my experience of your script, It worked fine for me. You can go check my methods in the video.

0
Lol, video is completely fail AswormeDorijan111 531 — 5y
0
Rude, At least i tried :C User#24025 0 — 5y
0
Well, im disabling my account. Its just that everyone seems mean. Bye :C User#24025 0 — 5y

Answer this question