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

Why am I getting this error in my datastore? (attempt to index local...)

I'm getting the error ServerScriptService.Datastore2:17: attempt to index local 'storedvalues' (a number value) and I'm not sure why

ServerScript 1

local DS = game:GetService("DataStoreService"):GetDataStore("test-001")

game.Players.PlayerAdded:Connect(function(plr)
	local hiddenstats = Instance.new("Folder")
	hiddenstats.Name = "hiddenstats"
	hiddenstats.Parent = plr
	
	local Energy = Instance.new("IntValue")
	Energy.Name = "Energy"
	Energy.Parent = hiddenstats
	Energy.Value = 100
	
	local key = "User-" .. plr.userId
	local storedvalues = DS:GetAsync(key)
	
	if storedvalues then
		Energy.Value = storedvalues[1] -- This is the source
		
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local items = (plr.hiddenstats.Energy.Value)
	local key = "User-" .. plr.userId
	
	DS:SetAsync(key, items)
end)

I'm not the best at datastores if somebody could explain why I'm having this error and how to fix it, that would be nice ^_^ .

1 comment

0

I know it's not leaderstats. I did that on purpose, so it wouldn't show up on the leaderboard.

1 answer

✓ Accepted answer

Lets do this logically:

The line thats causing the error is:

Energy.Value = storedvalues[1]

And roblox says:
attempt to index local 'storedvalues' (a number value)

What it's trying to say is:
You're tying to index this variable, which is actually a number value, so you can't do that

So, lets look at how you are saving this:

local items = (plr.hiddenstats.Energy.Value)

You will notice (plr.hiddenstats.Energy.Value), and you will notice that you are using the wrong kind of brackets.

You should be using {} not ().

Now, their are either two options you could choose to fix this:

Change local items = (plr.hiddenstats.Energy.Value) to local items = {plr.hiddenstats.Energy.Value}

OR

Change Energy.Value = storedvalues[1] to Energy.Value = storedvalues

This is because you are saving it as the datatype of a number and in order to use indexing, you need to use the data structure of an array.

I hope this helps.

2 comments

0

Thank you!

0

No problem

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