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 7 years ago
Edited 7 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:

01local DSService = game:GetService('DataStoreService'):GetDataStore('Hamburger223232')
02game.Players.PlayerAdded:connect(function(plr)
03    -- Define variables
04    print('waiting until tycoonscript ready')
05    wait(6)
06    print('Starting')
07    local uniquekey = 'id-'..plr.userId
08    local leaderstats = Instance.new('IntValue', plr)
09    local Moneystorage = game.ServerStorage:FindFirstChild('PlayerMoney')
10    local PlayerMoney = Moneystorage:FindFirstChild(plr.Name)
11    local savevalue =  PlayerMoney
12    --leaderstats.Name = 'leaderstats'
13    --savevalue.Parent = leaderstats
14    --savevalue.Name = 'Cash'
15 
View all 33 lines...

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 7 years ago
Edited 7 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 :

1local savevalue = PlayerMoney.Value
0
It works now Thanks for your help. Timmerman73 85 — 7y
Ad
Log in to vote
0
Answered by 7 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

1pcall(function()
2 
3end)

Answer this question