Why is this variable changing function not working

I wanted to make a weapon-switching function, but I seem to have a problem that I can’t fix.
I am firing this function in a server script, the rest of the code is done in a module script.

Module script :

local CharacterStats = {}
CharacterStats.__index = CharacterStats

local RF = game:GetService("ReplicatedFirst")
local RS = game:GetService("ReplicatedStorage")
local TS = game:GetService("TweenService")

local Assets = RF.Assets
local UI = Assets.UI

local WeaponList = require(RS.Modules.Lists.WeaponList)

local Players = game:GetService("Players")
local Character

function CharacterStats.new()
	local self = setmetatable({}, CharacterStats)

	self.Weapon = {
		Name = "Galfin Scythe";
		Type = "Scythe";
	}

	self.ArmorSet = {
		Head = nil;
		Torso = nil;
		Legs = nil;
	}

	self.Stat = {
		
	}
	
	self.Colors = {
		None = Color3.fromRGB(255, 255, 255);
		Physical = Color3.fromRGB(255, 106, 92);
		Magic =  Color3.fromRGB(98, 192, 255);
		Dexterity =  Color3.fromRGB(255, 193, 117);
		Holy =  Color3.fromRGB(255, 238, 0);
		Choatic =  Color3.fromRGB(255, 0, 111);
	}

	return self
end

function CharacterStats:GiveStats()
	
	self.Stat = WeaponList[self.Weapon.Type][self.Weapon.Name].WeaponStats
	
end

How should I go about making this work?

1 Like

Server Script :

local RS = game:GetService("ReplicatedStorage")

local ModuleStats = require(RS.CharacterStats)
local CharacterStats = ModuleStats.new()

CharacterStats:GiveStats()

I don’t see anything wrong with the script. Is it printing any errors? or maybe its the WeaponList module?

For some reason it get’s nil from self.Weapon.Type and also not
20:13:33.836 Scythe - Server - CharacterStats:49
20:13:37.615 ReplicatedStorage.CharacterStats:49: attempt to index nil with ‘Type’ - Server - CharacterStats:49
(Both from the same print)

What does the WeaponList module look like?

local Weapon = {
	["Sword"] = {
		["Wooden Sword"] = {
			Name = "Wooden Sword";
			Type = "Sword";
			Decal = 0;
			SP = 0;
			AttackSpeed = 1;
			
			WeaponStats = {
				["Physical Boost"] = 5;
			};
			
			Perks = {
				
			};
		};
		
		["Stone Sword"] = {
			Name = "Stone Sword";
			Type = "Sword";
			Decal = 0;
			SP = 15;
			AttackSpeed = 1;

			WeaponStats = {
				["Physical Boost"] = 13;
			};

			Perks = {

			};
		};
		
		["Steel Sword"] = {
			Name = "Steel Sword";
			Type = "Sword";
			Decal = 0;
			SP = 25;
			AttackSpeed = 1;

			WeaponStats = {
				["Physical Boost"] = 21;
			};

			Perks = {

			};
		};
		
		["Tempered Sword"] = {
			Name = "Tempered Sword";
			Type = "Sword";
			Decal = 0;
			SP = 40;
			AttackSpeed = 1;

			WeaponStats = {
				["Physical Boost"] = 32;
			};

			Perks = {

			};
		};
		
		["Ventus Blade"] = {
			Name = "Ventus Blade";
			Type = "Sword";
			Decal = 0;
			SP = 70;
			AttackSpeed = 1;

			WeaponStats = {
				["Wind Boost"] = 56;
				["Physical Boost"] = 15;
				["Dexterity Boost"] = 12;
			};

			Perks = {
				["Sweeping Winds"] = 0.1;
			};
		};
	};
	
	["Scythe"] = {
		["Galfin Scythe"] = {
			Name = "Galfin Scythe";
			Type = "Scythe";
			Decal = 0;
			SP = 0;
			AttackSpeed = 3;

			WeaponStats = {
				["Holy Boost"] = 100;
				["Dexterity Boost"] = 100;
			};

			Perks = {
				["Cleanse"] = 0.1;
			};
		};
	};
}

return Weapon

Can you try printing CharacterStats?

20:25:56.562 ▼ {
[“ArmorSet”] = {},
[“Colors”] = ▼ {
[“Choatic”] = 1, 0, 0.435294,
[“Dexterity”] = 1, 0.756863, 0.458824,
[“Holy”] = 1, 0.933333, 0,
[“Magic”] = 0.384314, 0.752941, 1,
[“None”] = 1, 1, 1,
[“Physical”] = 1, 0.415686, 0.360784
},
[“Stat”] = ▼ {
[“Dexterity Boost”] = 100,
[“Holy Boost”] = 100
},
[“Weapon”] = ▼ {
[“Name”] = “Galfin Scythe”,
[“Type”] = “Scythe”
}
} - Server - ServerScript:8

Huh thats weird… the stats still got set even though it errored.

It’s completely fine on the server side but if I have another function in the module use self.Stat it’s still empty.

function CharacterStats:CalculateDMG(Damage, Lvl, Torso)
		print(self.Stat) -- Empty here
		
		local DisplayDamage = 0
		
		if Torso:FindFirstChild("BillboardGui") then
			for i, v  in pairs(Torso:GetChildren()) do
				if v.Name == "BillboardGui" and v.TextLabel.TextColor3 == Color3.fromRGB(255, 255, 255) then
					DisplayDamage += v.TextLabel.Text
					v:Destroy()
				end
			end
		end

		local nBillboard = UI.BillboardGui:Clone()
		nBillboard.Parent = Torso

		nBillboard.TextLabel.TextColor3 = self.Colors.None
		nBillboard.TextLabel.Text = Damage + DisplayDamage
		nBillboard.StudsOffset = Vector3.new(math.random(-1, 1), math.random(2.5, 3), 0)

		local nTransparent = TS:Create(nBillboard.TextLabel, TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, 0, false, 1), {TextTransparency = 1})

		nTransparent:Play()

		nTransparent.Completed:Connect(function()
			nBillboard:Destroy()
		end)
		
		print(self.Stat) -- Empty here too

		for i, v in pairs(self.Stat) do
			
			print(i, v)

			local seperator = " "
			local array = string.split(i, seperator)

			if Torso:FindFirstChild("BillboardGui") then
				for i, v  in pairs(Torso:GetChildren()) do
					if v.Name == "BillboardGui" and v.TextLabel.TextColor3 == self.Colors[array[1]] then
						Damage += v.TextLabel.Text
						v:Destroy()
					end
				end
			end

			local StatDamage = CharacterStats:roundNumber((Damage/100)*v, 2)
			
			Damage += StatDamage

			local Billboard = UI.BillboardGui:Clone()
			Billboard.Parent = Torso

			Billboard.TextLabel.TextColor3 = self.Colors[array[1]]
			Billboard.TextLabel.Text = StatDamage
			Billboard.StudsOffset = Vector3.new(math.random(-1, 1), math.random(0.1, 0.5), 0)

			local Transparent = TS:Create(Billboard.TextLabel, TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, 0, false, 1), {TextTransparency = 1})

			Transparent:Play()

			Transparent.Completed:Connect(function()
				nBillboard:Destroy()
			end)

		end
		
	end
	
	return Damage
end

There isn’t any table called “Type”. The only tables I see are “Sword” and “Scythe”.

nvm, I just re read the script, I see you’re actually indexing “Scythe” however it says it’s nil

The weird part is I also put a print in the GiveStats function and it prints completely fine

function CharacterStats:GiveStats()
	
	print(self.Weapon)
	
	self.Stat = WeaponList[self.Weapon.Type][self.Weapon.Name].WeaponStats
	
	print(self.Stat)
	
end

Prints :
20:53:28.370 ▼ {
[“Name”] = “Galfin Scythe”,
[“Type”] = “Scythe”
} - Server - CharacterStats:46
20:53:28.370 ▼ {
[“Dexterity Boost”] = 100,
[“Holy Boost”] = 100
} - Server - CharacterStats:50

But the CalculateDMG function doesn’t