Help with my weird crafting system

I am currently making a crude crafting system that detects if the player has tools in their backpack, and then uses them and consumes them to craft a certain item, the whole crafting system is managed using a local function to save space and I have no idea how to actually detect if the player has a certain amount of specific items in a local function.


this is specific part of the local function that manages the actual crafting, i’ve thought of using a table to detect if the player has tools in their backpack, but then I realized that I have to make this local function compatible (if that is a phrase?), which means that I need a variable in my local function which manages how much scrap I need to craft a specific item, and how much gunpowder I need to craft it as well, as not all recipes are the same.

God i’ve tried to think of even a slight solution, but my brain just froze when thinking about one, I looked on the dev forum for probably 40 minutes now with no success or even step forward, I really need help or else I will have to scrap this crafting system (no pun intended), which would really be a shame and quite a hit in my morale for my game.

Here is the FULL script (please do not steal)

local ReplicatedStorage = game:GetService('ReplicatedStorage')
local AmmoFolder = ReplicatedStorage.CraftingSystemFolder.AmmoPress
local Players = game:GetService('Players')
local debounce = false
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local CraftingEvent = ReplicatedStorage.Remotes.CraftingEvent
local Preview
local PreviewPart
local craftdb = false
local ScrapCount = {}
local GunpowderCount = {}


script.Parent.MainPart.ClickDetector.MouseClick:Connect(function(plr)
	if debounce == false then
		debounce = true
		plr.PlayerGui.CraftingGui.AmmoPress.Visible = true
	else
		debounce = false
		plr.PlayerGui.CraftingGui.AmmoPress.Visible = false
	end
end)

local function TemplatePreview(identification,objectType,clonePosition,craftTime)
	if identification == objectType then
			Preview = clonePosition:Clone()
		PreviewPart = Instance.new('Part')
		PreviewPart.Size = Preview:FindFirstChild('Handle').Size * 1.2
		PreviewPart.Transparency = 1
			PreviewPart.Parent = workspace
		Instance.new('ClickDetector').Parent = PreviewPart
			PreviewPart.Anchored = true
			Preview.Parent = workspace
			Preview.Handle.TouchInterest:Destroy()
			PreviewPart.Position = Preview:FindFirstChild('Handle').Position
		for i,v in pairs(clonePosition:GetChildren()) do
			if v:IsA('BasePart') then
				v.Transparency = 0.5
				v.Anchored = true
				v.Position = script.Parent.MainPart.Position + Vector3.new(0,3,0)
			end
		end
		PreviewPart.ClickDetector.MouseClick:Connect(function(plr)
			for i,v in pairs(plr.Backpack:GetChildren()) do
				if v.Name == 'Scrap' then
					table.insert(ScrapCount,i,v)
				end
			end
			for i,v in pairs(plr.Backpack:GetChildren()) do
				if v.Name == 'Gunpowder' then
					table.insert(GunpowderCount,i,v)
				end
			end
				craftdb = true
					ReplicatedStorage.CraftingSystemFolder.HammerIcon:Clone().Parent = PreviewPart
					ReplicatedStorage.CraftingSystemFolder.CraftingSound:Clone().Parent = PreviewPart
					PreviewPart:FindFirstChild('CraftingSound'):Play()
					wait(craftTime)
					PreviewPart:FindFirstChild('HammerIcon'):Destroy()
				PreviewPart:FindFirstChild('CraftingSound'):Destroy()
				local FinishedPart = clonePosition:Clone()
				FinishedPart.Parent = workspace
				for i,v in pairs(FinishedPart:GetChildren()) do
					v.Anchored = false
					v.Transparency = 0
					v.Position = PreviewPart.Position + Vector3.new(0,3,0)
				end
				craftdb = false
		end)
	end
end

CraftingEvent.OnServerEvent:Connect(function(plr,identification)
	if craftdb == false then
	if Preview ~= nil then
		Preview:Destroy()
	end
	if PreviewPart ~= nil then
		PreviewPart:Destroy()
	end
	TemplatePreview(identification,'LightAmmo',AmmoFolder["Light Ammo (60)"],5)
	TemplatePreview(identification,'MediumAmmo',AmmoFolder["Medium Ammo (30)"],10)
		TemplatePreview(identification,'HeavyAmmo',AmmoFolder["Heavy Ammo (10)"],15)
		end
end)

I really do hate it when I cannot think of a solution and have to resort to people to write it for me, so I really do apologise.

2 Likes

Fixed via discord much love! have fun

2 Likes

Yep! and god I am glad of it, thank you so much