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

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

Asked by 5 years ago

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 ^_^ .

0
I know it's not leaderstats. I did that on purpose, so it wouldn't show up on the leaderboard. Bearturnedhuman 48 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago

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.

0
Thank you! Bearturnedhuman 48 — 5y
0
No problem abnotaddable 920 — 5y
Ad

Answer this question