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

How to prevent my datastore script from giving an Error?

Asked by 6 years ago
Edited 6 years ago

I have a script that saves Money from Players so they keep it the next time they join but it gives an error and I dont know how to fix it. Here is my script:

local DSService = game:GetService('DataStoreService'):GetDataStore('Hamburger223232')
game.Players.PlayerAdded:connect(function(plr)
    -- Define variables
    print('waiting until tycoonscript ready')
    wait(6)
    print('Starting')
    local uniquekey = 'id-'..plr.userId
    local leaderstats = Instance.new('IntValue', plr)
    local Moneystorage = game.ServerStorage:FindFirstChild('PlayerMoney')
    local PlayerMoney = Moneystorage:FindFirstChild(plr.Name)
    local savevalue =  PlayerMoney
    --leaderstats.Name = 'leaderstats'
    --savevalue.Parent = leaderstats
    --savevalue.Name = 'Cash'

    -- GetAsync
    local GetSaved = DSService:GetAsync(uniquekey)
    if GetSaved then
        savevalue.Value = GetSaved[1]
    else
        local NumbersForSaving = {savevalue.Value}
        DSService:SetAsync(uniquekey, NumbersForSaving)
    end
end)

game.Players.PlayerRemoving:connect(function(plr)
    local Moneystorage = game.ServerStorage:FindFirstChild('PlayerMoney')
    local PlayerMoney = Moneystorage:FindFirstChild(plr.Name)
    local savevalue =  PlayerMoney
local uniquekey = 'id-'..plr.userId
local Savetable = {savevalue.Value}
DSService:SetAsync(uniquekey, Savetable)        
end)

When i test it with an alt in game this is what shows in the output. ServerScriptService.Save Service :19: attempt to index local 'savevalue' (a number value)

If anybody could help that would be amazing '

2 answers

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

I think you’re trying to get the object of the value but not the value itself, which people make mistakes on a lot.

On line 11, change PlayerMoney into PlayerMoney.Value. It’d be like this :

local savevalue = PlayerMoney.Value
0
It works now Thanks for your help. Timmerman73 85 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

You should still wrap your data saving and loading in an error handler to make sure your script doesn’t error and stop working because it failed to save/load

You can use

Pcall for instance

pcall(function()

end)

Answer this question