Loot Box System Error!

Hey there! I am trying to make a loot box system using this video tutorial:

and I wrote everything out and I came across a couple bugs.
Item Module Script in ReplicatedStorage:

local m = {}

m.Rarities = {
	
	{
		name = "Legendary";
		rarity = 0.05;
		color = Color3.fromRGB(170, 0, 255);
	};
	{
		name = "Epic";
		rarity = 0.15;
		color = Color3.fromRGB(255, 0, 0);
	};
	{
		name = "Very Rare";
		rarity = 0.3;
		color = Color3.fromRGB(255, 170, 0);
	};
	{
		name = "Rare";
		rarity = 0.5;
		color = Color3.fromRGB(255, 255, 0);
	};
	{
		name = "Uncommon";
		rarity = 0.7;
		color = Color3.fromRGB(0, 170, 255);
	};
	{
		name = "Common";
		rarity = 0.9;
		color = Color3.fromRGB(0, 255, 0);
	};
}

m.Cases = {
	Classic = {
		items = {
			"Green Trail";
			"Blue Trail";
			"Yellow Trail";
			"Orange Trail";
			"Red Trail";
			"Purple Trail";
		}
	}
}

m.Items = {
	{
		name = "Green Trail";
		imageID = "6128040883";
		rarityType = "Common"
	};
	{
		name = "Blue Trail";
		imageID = "6128041800";
		rarityType = "Uncommon"
	};
	{
		name = "Yellow Trail";
		imageID = "6128038485";
		rarityType = "Rare"
	};
	{
		name = "Orange Trail";
		imageID = "6128040179";
		rarityType = "Very Rare"
	};
	{
		name = "Red Trail";
		imageID = "6128037610";
		rarityType = "Epic"
	};
	{
		name = "Purple Trail";
		imageID = "6128039197";
		rarityType = "Legendary"
	};
}

return m

case Module Script in the StarterGui

local m = {}

local items = require(game.ReplicatedStorage.Items)

local player = game.Players.LocalPlayer
local gui = player:WaitForChild("PlayerGui")
local ui = gui:WaitForChild("SpeedGUI")
local elements = gui:WaitForChild("Elements")

function m:getRarity(type)
	for a, b in ipairs(items.Rarities) do
		if b.name == type then
			return b
		end
	end
end

function m:createItem()
	--Clone object and Set Scaleable Properties
	local object = elements:WaitForChild("item"):Clone()
	local scaledSize = math.floor((object.Size.X.Scale * m.settings.cUI.AbsoluteSize.Y)+0.5)/m.settings.cUI.AbsoluteSize.X
	
	--Get and Set Types
	local types = {}
	for a,b in ipairs(m.settings.case.items) do
		for d,c in ipairs(items.Items) do
			if c.name == b then
				local r = m:getRarity(c.rarityType)
				for t = 1, r.rarity*10 do
					table.insert(types,c)
				end
			end
		end
	end
	
	--Select item
	local item = types[m.settings.prng:NextInteger(1,#types)]
	local itemType = m:getRarity(item.rarityType)
	
	--Set other properties
	object.BorderColor3 = itemType.color
	object.rarityMark.BackgroundColor3 = itemType.color
	object.rarityMark.rarity.Text = itemType.name
	object.Name = item.name
	object.itemImage.Image = "rbxassetid://" .. item.imageID
	
	--Set Position
	object.Position = (m.settings.lastObject and m.settings.lastObject.Position + UDim2.new(scaledSize,0,0,0) + m.settings.offset) or UDim2.new(-scaledSize/2,0,0.5,0)
	
	--Set Parent
	object.Parent = m.settings.cUI
	
	--Set few variables
	m.settings.lastObject = object
	table.insert(m.settings.objects, object)
end

function m:check()
	for a,b in ipairs(m.settings.objects) do
		local scaledSize = b.AbsoluteSize
		if b.AbsolutePosition.X <= -scaledSize.x+m.settings.offset.X.Offset then
			table:remove(m.settings.objects,a)
			b:Destroy()
			m:createItem()
			m:check()
		end
	end
end

function m:update()
	m.settings.speed = math.max(m.settings.speed-(0.03/m.settings.spinLength),0)
	m.settings.newAccel = m.settings.accel*math.sin((math.pi/2)*m.settings.speed)
	for a,b in ipairs(m.settings.objects) do
		b.Position = b.Position - UDim2.new(m.settings.newAccel/m.settings.cUI.AbsoluteSize.X,0,0,0)
	end
	if m.settings.newAccel == 0 then
		m:result()
	end
end

function m:result()
	--Disconnect
	m.render:disconnect()
	
	--Get selected item
	local item = nil
	for a,b in ipairs(m.settings.objects) do
		local left,right = b.AbsolutePosition.X/m.settings.cUI.AbsoluteSize.X,
			(b.AbsolutePosition.X + b.AbsoluteSize.X)/m.settings.cUI.AbsoluteSize.X
		if left <= 0.5 and right >= 0.5 then
			item = b
			break
		end
	end
	if item then
		m.newResult = item
	else	
		local closestItems = {}
		for a,b in ipairs(m.settings.objects) do
			table.insert(closestItems {
				mag = (Vector2.new(b.AbsolutePosition.X/m.settings.cUI.AbsoluteSize.X,0)-Vector2.new(0.5,0)).magnitude;
				item = b;
			})
		end
		table.sort(closestItems,function(a,b) return a.mag<b.mag end)
		m.newResult = closestItems[1].item
	end
end

function m:create(caseType)
	--Settings
	m.settings = {
		case = items.Cases[caseType];
		cUI = elements:WaitForChild("caseUI"):Clone();
		objects = {};
		prng = Random.new();
		offset = UDim2.new(0,25,0,0);
		accel = 30;
		speed = 1;
		spinLength = 5;
	}
	m.newResult = nil
	
	--PreSetup
	m.settings.cUI.Parent = ui
	for t = 1, 15 do
		m:createItem()
	end
	
	--Services
	m.render = game:GetService("RunService").RenderStepped:connect(function()
		m:update()
		m:check()
	end)
	--Wait for result
	repeat wait() until m.newResult
	
	--Get rid of ui
	spawn(function()
		wait(1)
		m.settings.cUI:TweenPosition(UDim2.new(0,0,1.15,0),"Out", "Sine", 1, true, function() m.settings.cUI:Destroy() end)
	end)
	return m.newResult
end

return m

Client local script in the StarterGui

local a = require(game.Players.LocalPlayer:WaitForChild("PlayerGui").Modules.case):create("Classic")
print(a)

Pic of the explorer: Screenshot 2021-03-14 120815

This is the error I am getting,


Line 62 and 133 are in the case script here

function m:check()
	for a,b in ipairs(m.settings.objects) do
		local scaledSize = b.AbsoluteSize
		if b.AbsolutePosition.X <= -scaledSize.x+m.settings.offset.X.Offset then
			table:remove(m.settings.objects,a) --This line is throwing the error
			b:Destroy()
			m:createItem()
			m:check()
		end
	end
end
m.render = game:GetService("RunService").RenderStepped:connect(function()
		m:update()
		m:check() --This line fires the function
	end)

This scripting is a little advanced for me and I don’t understand everything, so does anyone have any idea why I am getting that error. Isn’t “a”(or argument #2) inherently a number? Not a table?

Maybe try printing a and see what it prints out, also I don’t know if this’ll help, but shouldn’t

table:remove(m.settings.objects,a)

Be

table.remove(m.settings.objects,a)

Or do both work regardless

1 Like

So yes “a” is a table, but that doesn’t make sense, since isn’t it the integer in the for loop?

Could you show me what the objects table contains

Actually scratch what I said before, “a” isn’t actually a table, it prints “1”, and the m.settings.objects prints

[1] = Red Trail,
                    [2] = Orange Trail,
                    [3] = Green Trail,
                    [4] = Blue Trail,
                    [5] = Yellow Trail,
                    [6] = Blue Trail,
                    [7] = Green Trail,
                    [8] = Green Trail,
                    [9] = Blue Trail,
                    [10] = Yellow Trail,
                    [11] = Yellow Trail,
                    [12] = Green Trail,
                    [13] = Green Trail,
                    [14] = Blue Trail,
                    [15] = Green Trail

not sure why the top one won’t indent

So it prints it as a string instead of a number if I’m understanding you correctly?

No it prints a number I just happened to put quotations around it, it prints the number 1

So it should be working now or is there another error?

No, same error, it still says that the second argument is not valid because it is a table, not a number. even though “a” IS a number, “a” being the 2nd argument. Sorry for not explaining that correctly…

Which doesn’t make sense at all… it is a number when it is saying it is a table

That’s quite odd, maybe again it could be that used : instead of a ., although I’m not sure if by now you replaced it

You’re calling a method by using “:” instead of a .

Therefore your second argument is going to automatically be “self” which is whatever you’re calling it on. In this case its the “m.settings.objects” table. You should be able to remove the second argument completely (a) and still get the same error that you were receiving before. So just do as embat suggested and change it from : to .

OOOOOOOOOOOOOOHHHHH… I did not realize that you gave me the solution up at the top, I thought the

two lines that you put were identical, I didn’t notice the difference. THANK YOU!

Anytime! If you have anymore issues don’t be afraid to make another post! And yea, it would’ve been kinda hard to see the difference haha

1 Like