Function Parameters Acting Weird

Hi there! I had a problem where whenever I require a module script and run a function, it says the parameter is a table but it should be 1. Here is the code (line 12)

local module = {}

module.Rarities = {
	["Common"] = {["Color"] = Color3.fromRGB(143, 143, 143), ["ChanceRange"] = Vector2.new(250,1000),},
	["Uncommon"] = {["Color"] = Color3.fromRGB(60, 220, 42), ["ChanceRange"] = Vector2.new(200,246), ["UpgradePrice"] = function(level) return level*(15+level*2) end,},
	["Rare"] = {["Color"] = Color3.fromRGB(114, 58, 255), ["ChanceRange"] = Vector2.new(130,199), ["UpgradePrice"] = function(level) return level*level(15+level*2) end,},
	["Ancient"] = {["Color"] = Color3.fromRGB(255, 238, 0), ["ChanceRange"] = Vector2.new(90,129), ["UpgradePrice"] = function(level) return level*level(15+level*2) end,},
	["Legendary"] = {["Color"] = Color3.fromRGB(255, 153, 28), ["ChanceRange"] = Vector2.new(40,89), ["UpgradePrice"] = function(level) return level*level(15+level*2) end,},
	["Mythic"] = {["Color"] = Color3.fromRGB(35, 152, 255), ["ChanceRange"] = Vector2.new(10,39), ["UpgradePrice"] = function(level) return level*level(15+level*2) end,},
	["Prime"] = {["Color"] = Color3.fromRGB(255, 0, 4), ["ChanceRange"] = Vector2.new(0,9), ["UpgradePrice"] = function(level) return level*level(15+level*2) end,},
}
module.Rarities.Common.UpgradePrice = function(level)
	print(level)
	return level*15
end

return module

and here is the activator

print(require(game.ReplicatedStorage.Modules.PetHandler).Rarities.Common:UpgradePrice(1))

Thanks!

Why did you put . for the function in the module and : for when you called it

Do:

print(require(game.ReplicatedStorage.Modules.PetHandler).Rarities.Common.UpgradePrice(1))

It’s so the self object is implied as the first argument received by the method.
https://www.lua.org/pil/16.html

2 Likes
local module = {}

module.Rarities = {
	["Common"] = {["Color"] = Color3.fromRGB(143, 143, 143), ["ChanceRange"] = Vector2.new(250,1000),},
	["Uncommon"] = {["Color"] = Color3.fromRGB(60, 220, 42), ["ChanceRange"] = Vector2.new(200,246), ["UpgradePrice"] = function(level) return level*(15+level*2) end,},
	["Rare"] = {["Color"] = Color3.fromRGB(114, 58, 255), ["ChanceRange"] = Vector2.new(130,199), ["UpgradePrice"] = function(level) return level*level(15+level*2) end,},
	["Ancient"] = {["Color"] = Color3.fromRGB(255, 238, 0), ["ChanceRange"] = Vector2.new(90,129), ["UpgradePrice"] = function(level) return level*level(15+level*2) end,},
	["Legendary"] = {["Color"] = Color3.fromRGB(255, 153, 28), ["ChanceRange"] = Vector2.new(40,89), ["UpgradePrice"] = function(level) return level*level(15+level*2) end,},
	["Mythic"] = {["Color"] = Color3.fromRGB(35, 152, 255), ["ChanceRange"] = Vector2.new(10,39), ["UpgradePrice"] = function(level) return level*level(15+level*2) end,},
	["Prime"] = {["Color"] = Color3.fromRGB(255, 0, 4), ["ChanceRange"] = Vector2.new(0,9), ["UpgradePrice"] = function(level) return level*level(15+level*2) end,},
}
module.Rarities.Common.UpgradePrice = function(self, level)
	print(level)
	return level*15
end

return module
2 Likes

I knew about the self stuff but i thought it was the reason it was erroring

Your original suggestion is likely correct, he’s probably misusing the colon operator. Just in case he isn’t though, since he defined the function with the dot operator he’ll need to receive an additional parameter to handle the self object.

Thanks for the help! do you happen to know why this line

module.Rarities.Uncommon.UpgradePrice = function(self, level) return level*(15+level*2) end

prints this
image
, but this line

module.Rarities.Rare.UpgradePrice = function(self, level) return level*level*(15+level*2) end

errors with this -

Edit- i think it has something to do with the formulas after the return.

return (level*(level*(15+(level*2)))) end

Hmmm. It still gives the same error. On second thought it isn’t the formula because the one from the Uncommon rarity doesn’t make it work. Thanks for your patience though. If you would like to help me more, here is the full script.

local module = {}

module.Rarities = {
	["Common"] = {["Color"] = Color3.fromRGB(143, 143, 143), ["ChanceRange"] = Vector2.new(250,1000),},
	["Uncommon"] = {["Color"] = Color3.fromRGB(60, 220, 42), ["ChanceRange"] = Vector2.new(200,249),},
	["Rare"] = {["Color"] = Color3.fromRGB(114, 58, 255), ["ChanceRange"] = Vector2.new(130,199),},
	["Ancient"] = {["Color"] = Color3.fromRGB(255, 238, 0), ["ChanceRange"] = Vector2.new(90,129),},
	["Legendary"] = {["Color"] = Color3.fromRGB(255, 153, 28), ["ChanceRange"] = Vector2.new(40,89),},
	["Mythic"] = {["Color"] = Color3.fromRGB(35, 152, 255), ["ChanceRange"] = Vector2.new(10,39),},
	["Prime"] = {["Color"] = Color3.fromRGB(255, 0, 4), ["ChanceRange"] = Vector2.new(0,9),},
}
module.Rarities.Common.UpgradePrice = function(self, level) return level*15 end
module.Rarities.Uncommon.UpgradePrice = function(self, level) return level*(15+level*2) end
module.Rarities.Rare.UpgradePrice = function(self, level) return level*(15+level*2) end
module.Rarities.Ancient.UpgradePrice = function(self, level) return (level*(level*(15+(level*2)))) end
module.Rarities.Legendary.UpgradePrice = function(self, level) return (level*(level*(15+(level*2)))) end
module.Rarities.Mythic.UpgradePrice = function(self, level) return (level*(level*(15+(level*2)))) end
module.Rarities.Prime.UpgradePrice = function(self, level) return level*level*(15+level*2) end

return module

Whenever I call the Common upgrade price at level 1 it works and also with the uncommon rarity. Once it reaches Rare it doesnt work.

Edit- the rare formula is the same as uncommon and it still isnt working

What’s that line of code shown in the error?

Here is the error. It says line 6

The 4th line down, in that image, is the error pertaining to that line of code?

return level*level*15+level*2 end

Let me know if this changes anything.

Oh sorry for the misunderstanding. The 4th line down is the activator. I don’t think that’s the problem because all I did was change the name of the rarity
here is the code for uncommon -
for u = 1,10,1 do warn("Level;",u," - Cost;",require(game.ReplicatedStorage.Modules.PetHandler).Rarities.Uncommon.UpgradePrice(u)," Souls") end

and here is the rare activator
for u = 1,10,1 do warn("Level;",u," - Cost;",require(game.ReplicatedStorage.Modules.PetHandler).Rarities.Rare.UpgradePrice(u)," Souls") end

They basically print a chain of how much each level costs

no it didnt. It may be the activator since thats the only thing thats changing.

If it means anything, the common prints like this
image
I have no idea why its printing in white there shold be no line of code that does that

and this is the print for uncommon
image

Important update - I restarted studio and now none are working