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
local RS = game:GetService("ReplicatedStorage")
local ModuleStats = require(RS.CharacterStats)
local CharacterStats = ModuleStats.new()
CharacterStats:GiveStats()
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)
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