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

Attempt to index a nil value?

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

1 comment

0

I think the problem is you mispelled line 28.

1 answer

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

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