Attempt to index nil with Stats

I have a module script, and I want the TotalWeight value to be added with the TotalLuck stat I have

ModuleScripts cant get the local player, but any solution I’ve tried such as using playeradded to get the player have not worked

All I need is for the script to read the data and add it to the TotalWeight value, any help is apreciated

local RandomSeed = Random.new()

local RaritySystem = {}

function RaritySystem.TotalWeight(Rarities)
	local TotalWeight = 0 + game.Players.LocalPlayer.Stats.TotalLuck.Value
	for i, item in ipairs(Rarities) do
		TotalWeight = TotalWeight + item.weight
	end
	return TotalWeight
end

function RaritySystem.SelectRarity(Rarities)
	local RandomValue
	local SelectedRarity
	local TotalWeight = RaritySystem.TotalWeight(Rarities)
	repeat
		RandomValue = RandomSeed:NextNumber(0, 1)

		for _, Rarity in ipairs(Rarities) do
			if RandomValue <= Rarity.weight / TotalWeight then
				SelectedRarity = Rarity
				break
			else
				RandomValue = RandomValue - Rarity.weight / TotalWeight
			end
		end

		task.wait()
	until SelectedRarity ~= nil

	return SelectedRarity
end




function RaritySystem.CalculateAndDisplayChances(Rarities)
	local TotalWeight = RaritySystem.TotalWeight(Rarities)
	for _, Rarity in ipairs(Rarities) do
		local chance = (Rarity.weight / TotalWeight) * 100
		warn(Rarity.name .. ": " .. tostring(string.format("%.4f", chance)) .. "% chance")
	end
end

return RaritySystem

Well what are you using this for because if it’s a display you can just send a remote event when a player joins to show the chances or if it is to actually open then you could just receive a client event from a remote event

its not a display i need it to calculate the total weight based off a number in the player data

Then just do it when the player joins…

ive tried that but it returns errors

What error? If it couldn’t find stats then either your still using Local Player or it’s not been added in

local RandomSeed = Random.new()

local RaritySystem = {}

game.Players.PlayerAdded:Connect(function(plr)
	function RaritySystem.TotalWeight(Rarities)
		local TotalWeight = 0 + plr:WaitForChild("Stats").TotalLuck.Value
		for i, item in ipairs(Rarities) do
			TotalWeight = TotalWeight + item.weight
		end
		return TotalWeight
	end
end)


function RaritySystem.SelectRarity(Rarities)
	local RandomValue
	local SelectedRarity
	local TotalWeight = RaritySystem.TotalWeight(Rarities)
	repeat
		RandomValue = RandomSeed:NextNumber(0, 1)

		for _, Rarity in ipairs(Rarities) do
			if RandomValue <= Rarity.weight / TotalWeight then
				SelectedRarity = Rarity
				break
			else
				RandomValue = RandomValue - Rarity.weight / TotalWeight
			end
		end

		task.wait()
	until SelectedRarity ~= nil

	return SelectedRarity
end




function RaritySystem.CalculateAndDisplayChances(Rarities)
	local TotalWeight = RaritySystem.TotalWeight(Rarities)
	for _, Rarity in ipairs(Rarities) do
		local chance = (Rarity.weight / TotalWeight) * 100
		warn(Rarity.name .. ": " .. tostring(string.format("%.4f", chance)) .. "% chance")
	end
end

return RaritySystem

you would call game.Players.PlayerAdded out side of the module script then call the function and pass the player as a parameter

and how exactly should I do that?

A server script in server script service. Then just do

local mymodule = require(--path to module here)

game.Players.PlayerAdded:Connect(function(player)

  local TotalWeight = mymodule.TotalWeight(player)

end)


Then in the module script you can already have the rarities in a different table somewhere

I did this, but its not calculating

local mymodule = require(game.ServerStorage.Luck)

	game.Players.PlayerAdded:Connect(function(player)

		local TotalWeight = mymodule.TotalWeight(player:FindFirstChild("Stats").TotalLuck.Value)

	end)

how about do :WaitForChild("Stats") instead of :FindFirstChild and are there any errors?

So now as I said replace Rarities with a table in the module script and change the parameter to be player or plr

Why would I need a table I already have all my stats listed in other scripts I strictly just need to calculate the total weight value and nothing else, what do you mean by change the parameter?

for the module script you can just do

local RandomSeed = Random.new()

local Rarities = {} -- put your rarities here

local RaritySystem = {}

function RaritySystem.TotalWeight(plr,rarities)
       if not rarities then
       rarities = Rarities
       end

		local TotalWeight = 0 + plr:WaitForChild("Stats").TotalLuck.Value
		for i, item in ipairs(rarities) do
			TotalWeight = TotalWeight + item.weight
		end
		return TotalWeight
end



function RaritySystem.SelectRarity(Rarities)
	local RandomValue
	local SelectedRarity
	local TotalWeight = RaritySystem.TotalWeight(Rarities)
	repeat
		RandomValue = RandomSeed:NextNumber(0, 1)

		for _, Rarity in ipairs(Rarities) do
			if RandomValue <= Rarity.weight / TotalWeight then
				SelectedRarity = Rarity
				break
			else
				RandomValue = RandomValue - Rarity.weight / TotalWeight
			end
		end

		task.wait()
	until SelectedRarity ~= nil

	return SelectedRarity
end




function RaritySystem.CalculateAndDisplayChances(Rarities)
	local TotalWeight = RaritySystem.TotalWeight(Rarities)
	for _, Rarity in ipairs(Rarities) do
		local chance = (Rarity.weight / TotalWeight) * 100
		warn(Rarity.name .. ": " .. tostring(string.format("%.4f", chance)) .. "% chance")
	end
end

return RaritySystem

Ok and what is the code in that script?

local x = require(game.ReplicatedStorage.EternityNum)	
local config = script.Parent.config
script.Parent.part.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChildOfClass("Humanoid") then
		if game.Players:GetPlayerFromCharacter(hit.Parent) then
			local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
			local s = plr:FindFirstChild("Stats")
			local money = plr.Stats:FindFirstChild("Money")
			local multi = plr.Stats:FindFirstChild("Multi")
			local common = plr.Stats:FindFirstChild("Common")
			local uncommon = plr.Stats:FindFirstChild("Uncommon")
			local rare = plr.Stats:FindFirstChild("Rare")
			local epic = plr.Stats:FindFirstChild("Epic")
			local legendary = plr.Stats:FindFirstChild("Legendary")
			local ro = plr.Stats:FindFirstChild("RunesOpened")
			local mvp = plr.Stats:FindFirstChild("MVP")
			local rg = plr.Stats:FindFirstChild("RuneMaster")
			if multi == nil then return false end
			if x.meeq(x.convert(multi.Value),(x.convert(config.cost.Value))) then
				local s = x.convert(multi.Value) 
				local v = x.convert(config.cost.Value) 
				s = x.sub(s,v) 
				multi.Value = x.bnumtostr(s)
				local RarityList = require(game.ServerStorage:FindFirstChild("Luck"))

				local RarityWeights = {
					{name = "Common", weight = 70},
					{name = "Uncommon",  weight = 25},
					{name = "Rare", weight = 4},
					{name = "Epic", weight = 2},
					{name = "Legendary", weight = 0.01},
				}

				local SelectedRarity = RarityList.SelectRarity(RarityWeights) -- Gives rarity
					local stt = plr.Stats:FindFirstChild(SelectedRarity.name)
					local svb = x.convert(stt.Value)
					local val =x.convert(config.amount.Value)

					local val = x.mul(val, (x.add(x.mul(x.convert(mvp.Value),{1,0}),{1,0})))
					local val = x.mul(val, (x.add(x.mul(x.convert(rg.Value),{2,0}),{1,0})))

					svb = x.add(svb, val)
					stt.Value = x.bnumtostr(svb)
					ro.Value = ro.Value + 1				
				
			end
		end
	end
end)

local svb = x.convert(config.cost.Value)
local sv = x.convert(config.amount.Value)
script.Parent.part.BillboardGui.Cost.Text = "Req: "..x.short(svb).." Rebirths"-- just change the stuff between the ""
script.Parent.part.BillboardGui.Ammt.Text = x.short(sv).." Basic Rune"

just pass the player as the first argument through both