Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to prevent my datastore script from giving an Error?

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

✓ Accepted answer

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

1 comment

0

It works now Thanks for your help.

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.