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

Attempt to index a nil value?

Asked by 4 years ago

I am making a stats system and its returning nil and I dont quite get it?

Here's my code:

local DataStore2 = require(1936396537)

DataStore2.Combine("MasterKey", "Stats")

local function setDataTable()
    local UserData = {
        Stats = {
            ["Strenght"] = 1,
            ["Agility"] = 1,
            ["Vitality"] = 1
        }
    }
    return UserData
end

game.Players.PlayerAdded:Connect(function(player)
    local UserData = DataStore2("MasterKey", player):Get(setDataTable())

    local PlayerStats = Instance.new("Folder", player); PlayerStats.Name = "PlayerStats"

    local Strenght = Instance.new("IntValue", PlayerStats); Strenght.Name = "STR"
    local Agility = Instance.new("IntValue", PlayerStats); Agility.Name = "AGI"
    local Vitality = Instance.new("IntValue", PlayerStats); Vitality.Name = "VIT"

    local StatsData = DataStore2("Stats",player)

    local function UpdateStats(UpdatedValue)
        Strenght.Value = StatsData:Get(UpdatedValue).Strenght
        Agility.Value = StatsData:Get(UpdatedValue).Agility
        Vitality.Value = StatsData:Get(UpdatedValue).Vitality
    end
    UpdateStats(UserData.Stats)
    StatsData:OnUpdate(UpdateStats)
end)

It returns

ServerScriptService.ServerScripts.StatusDataStore:28: attempt to index a nil value
0
I think the problem is you mispelled line 28. firestarroblox123 440 — 4y

1 answer

Log in to vote
1
Answered by 4 years ago

I'm unsure what you're trying to do on line 28-30 but I can infer.

Since you're using DataStore2, the "UpdateStats" function passes in the updated value for the key "Stats". "Stats" should be what is declared on line 7 so we should be able to change lines 28-30 to something like:

Strenght.Value = UpdatedValue.Strenght
Agility.Value = UpdatedValue.Agility
Vitality.Value = UpdatedValue.Vitality
Ad

Answer this question