Having issues copying a gears using module

I wanna get the location thoughout the module where the item is, then copies the item to the player’s backpack. At the moment i get a error on line 35.

The issue i am currently facing is that he cannot copy the item. I have tried certain prints to find out the issue but I cannot put my finger on it what the issue is. I know it can find the item but it just does not wanna copy it over to the player’s inventory.

The item that should give the player a item when you touch it:

--- services
local playerService = game:GetService("Players")
--- locations
local LootboxSystemFolder = game.ReplicatedStorage.LootboxSystemFolder
local model = script.Parent
--- module locations & gear folder olcation
local LootboxSystemModule = require(LootboxSystemFolder.LootboxSystemModule)
local ItemFolder = game.ReplicatedStorage.LootboxSystemFolder.Gears

--local mathrandom = math.random(1,4)
local mathrandom = 1

---inside module locations
local recievedInfoOfItemFromGearFolder = LootboxSystemModule.Lootboxes["[1]NeutralBox"]["BoxItem" .. mathrandom].Item
local recievedInfoOfItemName = LootboxSystemModule.Lootboxes["[1]NeutralBox"]["BoxItem" .. mathrandom].Name
local boxChosen =  LootboxSystemModule.Lootboxes["[1]NeutralBox"]["BoxItem" .. mathrandom]

local function ModelTouched(touchedpart)
	local character = touchedpart.parent
	

	local debounce = false
	local humanoid = character:FindFirstChildWhichIsA("Humanoid")
	
	if humanoid and debounce == false then
		debounce = true
		local player = playerService:GetPlayerFromCharacter(character)

		--print(boxChosen) 
		--print(recievedInfoOfItemFromGearFolder)
		
		if recievedInfoOfItemFromGearFolder ~= nil then
			--print("Found item")
			local recievedInfoOfItemFromGearFolder2 = LootboxSystemModule.Lootboxes["[1]NeutralBox"]["BoxItem" .. mathrandom].Item
			local getGear = ItemFolder.recievedInfoOfItemFromGearFolder2:Clone() 
			getGear = player.Backpack
			--recievedInfoOfItemFromGearFolder2.parent = character.parent
			
		elseif recievedInfoOfItemFromGearFolder == nil then
			print("Could not find content to give on the Item: ".. recievedInfoOfItemName)
			
		end
		
		debounce = false
		--model:Destroy()
	end
	
end

model.Touched:Connect(ModelTouched)

The module:

local LootboxSystemModule = {}
local ItemFolder = game.ReplicatedStorage.LootboxSystemFolder.Gears

local Lootboxes = {
	["[-1]UnluckyBox"] = {

	},
	["[0]HorribleBox"] = {

	},
	["[1]NeutralBox"] = {
		["BoxItem1"] = {
			Name = "Horrible gun",
			Picture = "http://www.roblox.com/asset/?id=2579660936",
			Item = ItemFolder.Pew
		},
		["BoxItem2"] = {
			Name = "Test Item2",
			Picture = "http://www.roblox.com/asset/?id=2579660936",
			Item = nil
		},
		["BoxItem3"] = {
			Name = "Test Item3",
			Picture = "http://www.roblox.com/asset/?id=2579660936",
			Item = nil
		},
		["BoxItem4"] = {
			Name = "Test Item4",
			Picture = "http://www.roblox.com/asset/?id=2579660936",
			Item = nil
		},

	},
	["[2]MetalBox"] = {

	},
	["[3]SilverBox"] = {

	},
	["[4]GoldenBox"] = {

	},
	["[5]DiamondBox"] = {

	},
	["[6]OmegaMemeBox"] = {

	},
}

LootboxSystemModule.Lootboxes = Lootboxes

return LootboxSystemModule

Game locations of the item with the script:
image

Module location:
image

Thanks for reading, have a nice day!

1 Like

What error appears?
(also, .Touched events don’t work on models)

It would give me a better judgement for the script if you could tell me the error but try this:

--//Services
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

--//Modules
local LootboxSystemModule = require(ReplicatedStorage.LootboxSystemFolder.LootboxSystemModule)

--//Variables
local LootboxSystemFolder = ReplicatedStorage.LootboxSystemFolder
local ItemFolder = LootboxSystemFolder.Gears
local Part = script.Parent

--//Controls
local debounce = false
--local RandomNumber = Random.new():NextInteger(1, 4)
local randomNumber = 1
local recievedInfoOfItemFromGearFolder = LootboxSystemModule.Lootboxes["[1]NeutralBox"]["BoxItem" .. randomNumber].Item
local recievedInfoOfItemName = LootboxSystemModule.Lootboxes["[1]NeutralBox"]["BoxItem" .. randomNumber].Name
local boxChosen =  LootboxSystemModule.Lootboxes["[1]NeutralBox"]["BoxItem" .. randomNumber]

--//Functions
Part.Touched:Connect(function(hit)
	local Player = Players:GetPlayerFromCharacter(hit.Parent)
	
	if not Player or debounce then
		return
	end
	
	debounce = true
	
	if recievedInfoOfItemFromGearFolder then
		local recievedInfoOfItemFromGearFolder2 = LootboxSystemModule.Lootboxes["[1]NeutralBox"]["BoxItem" .. randomNumber].Item
		
		local newGear = recievedInfoOfItemFromGearFolder2:Clone()
		newGear.Parent = Player.Backpack
	else
		print("Could not find content to give on the Item: ".. recievedInfoOfItemName)
	end
	
	debounce = false
end)
1 Like

This works perfectly, thank you!

I’m just mirrorally confused why it would work now as I did use gear.parent and player.backpack, also pretty curious what the diffrent is between math.random and Random.new():NextInteger(number,number). Cause it seems to be math.random be with extra steps?

Also apologies for leaving some info out, error was related to copying to the player inventory as apparently gear.parent wouldn’t work (but did work for me in past).

It’s because instead of YourGear:Clone(), you did Folder.YourGear:Clone(), which wouldn’t work because YourGear was already an instance.

I use Random.new() as it has more functions, and math.random actually uses part of the backend for Random.new() afaik.

1 Like

Ah i see, ye my apologies. Been so busy scripting that I made this minor mistake and did not even see it myself. I’m happy you could figure it out, it means alot!

And about random.new() and math.random, welp I know math.random is pretty old as I used it around 2013 or even 2014. However I notice that if you try to print random.new() it wouldn’t give any value and just print random until you type :nextinteger.

image

1 Like

Yes, Random.new() is just the main object for it. All the other functions are the things that let it return something.

Random.new(n) can also be done, n being the seed for it’s random generation.

There are many functions for it, here is a list of them:

1 Like

Ah that’s amazing! I will consider trying to use that in my future projects or objects I make on Roblox. Thank you again for you amazing help!

1 Like

No problem, if you have any more questions, feel free to ask.

Probably an offtopic one but, do you desire to be friends? I gladly learn more from you and like to know you better. Ofcourse depends on you, I understand if you do not desire that!

Sure. I can help you whenever.

2 Likes

Thank you for being so open, ur a big help to this community!