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

Getting a "attempt to index a nil value" error in my DataStore2?

The output tells me there's an error line 54 of my script, but I don't quite understand why?


local DataStore2 = require(game.ServerScriptService:WaitForChild("DataModule")) --Gets DataStore Module
local MainKey = "MainKey" -- The Key
DataStore2.Combine(MainKey,"Stats","Achievment")

--DataTable

local function SetDataTable()-- Setting Variables In A Table
	local UserData = {
		Stats = {
			["Kills"] = 0,
			["Deaths,"] = 0,
			["XP"] = 0,
			["Level"] = 1,
			["Coins"] = 0,
			
		},
		
		
	}
end


game.Players.PlayerAdded:Connect(function(plr) -- Does Function When Player Joins
	local UserData = DataStore2(MainKey,plr) --Gets Table
	
	
	local leaderstats = Instance.new("Folder") ---Leaderstats!--
	leaderstats.Name = "leaderstats"
	
	local kills = Instance.new("IntValue")
	kills.Name = "Kills"	
	
	local deaths = Instance.new("IntValue")
	deaths.Name = "Deaths"
	
	local xp = Instance.new("IntValue")
	xp.Name = "Xp"
	
	local xpn = Instance.new("IntValue")
	xpn.Name = "Xpn"
	
	local level = Instance.new("IntValue")
	level.Name = "Level"
	
	local coins = Instance.new("IntValue")
	coins.Name = "Coins"
	
	local gems = Instance.new("IntValue")
	gems.Name = "Gems"
	
	local StatsData = DataStore2("Stats",plr)
	
	local function UpdateStats(updatedValue) --Updates Data
		kills.Value = StatsData:Get(updatedValue).kills
		deaths.Value = StatsData:Get(updatedValue).deaths
		xp.Value = StatsData:Get(updatedValue).xp
		xpn.Value = StatsData:Get(updatedValue).xpn
		level.Value = StatsData:Get(updatedValue).level
		coins.Value = StatsData:Get(updatedValue).coins
		gems.Value = StatsData:Get(updatedValue).gems		
	end
	
	
	
	UpdateStats(UserData.Stats) -- updates data and calls the function
		
	StatsData:OnUpdate(UpdateStats) -- calls func on update
	
	--Parenting Properties
	leaderstats.Parent = plr
	kills.Parent = leaderstats
	deaths.Parent = leaderstats
	xp.Parent = leaderstats
	level.Parent = leaderstats
	coins.Parent = leaderstats
	gems.Parent = leaderstats
	
	--Giving Stats
	
	while true do
		wait(200)
		local KeyData = DataStore2(MainKey)
		KeyData:Save()
	end
	
	local function LevelUp()
		print("test")
	end
end)

2 comments

0

I don't understand why there's an error!?

0

Is there a problem with the function? Or with the Values?

1 answer

I figured out the problem,

I forgot to get the DataTable in UserData

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