Way to check the Table

I am making this attachment system and I was wondering if I could make a script/function or perhaps a thing inside the script that would check for Attachments within the table instead of having to manually write what happens if there is a Attachments inside the Attachments table

what the table looks like:

Attachments = {
		Required = {
			Pistolgrip = {
				name = "AK-74 bakelite pistol grip",
				Type = "Pistolgrip",
				AttachedTo = "weapon",				
			},
			Handguard = {
				name = "AKS-74U Zenit B-11 handguard",
				Type = "Handguard",
				AttachedTo = "weapon",
				Attachments = {
					Slot1 = {
						Name = "",
						Type = "",
						AttachedTo = "AKS-74U Zenit B-11 handguard",
						Node = "Tactical1",
					},
					Slot2 = {
						Name = "",
						Type = "",
						AttachedTo = "AKS-74U Zenit B-11 handguard",
						Node = "Foregrip",
					},
					Slot3 = {
						Name = "Kiba Arms 25mm accessory ring mount",
						Type = "Mount",
						AttachedTo = "AKS-74U Zenit B-11 handguard",
						Node = "Tactical2",
						Attachments = {
							Slot1 = {
								Name = "Armytek Predator Pro v3 XHP35 HI flashlight",
								Type = "Tactical",
								AttachedTo = "Kiba Arms 25mm accessory ring mount",
								Node = "Tactical",
							},
						}
					},
					Slot4 = {
						Name = "",
						Type = "Tactical/Optic"
					},
				},
				
			},
		};				
	};

If I could make the script check each slot automatically for Attachments, since some attachments are mounts which You will be able to mount Tactical Devices, meaning those mounts will also contain Attachments table.

the script:

for i,v in pairs(Settings.Attachments.Required) do
	if Engine.GunMods:FindFirstChild(i):FindFirstChild(weapon.Name):FindFirstChild(v.name) then					
		local Attachment =  Engine.GunMods:FindFirstChild(i):FindFirstChild(weapon.Name):FindFirstChild(v.name):Clone()
		local weldTo= Attachment:FindFirstChild("weldTo")
		Attachment.Parent = weapon

		if v.AttachedTo == "weapon" then
			Attachment:SetPrimaryPartCFrame(weapon.Nodes[weldTo.Value].CFrame)
		else
			Attachment:SetPrimaryPartCFrame(weapon[Slots.AttachedTo].Nodes[Slots.Node].CFrame)
		end			

		if v.Attachments then
			for _,Slots in pairs(v.Attachments) do
				if Slots.Name ~= "" then
					local SlotAttachment = Engine.GunMods[Slots.Type][weapon.Name][Slots.Name]:Clone()
					SlotAttachment.Parent = weapon

					SlotAttachment:SetPrimaryPartCFrame(weapon[Slots.AttachedTo].Nodes[Slots.Node].CFrame)
				end
			end
			if v.Attachments.Attachments then
				--- same thing repeats
			end
		end
	end		
end

Currently I have to make it manually check for Attachments within Attachments

if You need more details or have questions feel free to ask Me.

1 Like

This is a bit difficult to read, so pardon if this not what you’re looking for. But if I understand correctly, you can create a function to return the value of AttachedTo, and handle the logic from there.

1 Like

I see, it is alright.

AttachedTo is kind of hard to explain for me.

AttachedTo is the Attachment Models name.

since some Attachments (example: Handguards, in this case the model opened on the screen) might also have slots to add stuff like lasers and flashlights (slot positions are located inside nodes folder). I added AttachedTo as a way for the script to find Which Attachment Model’s node inside of the weapon model it should set primarypartcframe to.

1 Like

I think what you’re looking for is a recursive function, here’s an example:

-- Example 
local function searchFunction(t:{})
	for i, v in pairs(t) do
		-- Insert your main function here
		print('skibdi')
				
		-- Conditional
		if v.Attachment then
			searchFunction(v.Attachment) -- Propagate current function to next layer for as many times possible
		end
	end
end
searchFunction(insertYourTableHere)

Idea being you call the function once manually (on the main table) and it will check for the condition you want, and if the conditon is met then it will then the function will continue to call itself on the next “layer” in the table.

Hopefully this helps or gives you a direction to go in

2 Likes

It works!

Thank you so much I would’ve never figured this out. I’ve never seen a function call itself, I didn’t think functions could even call themselves so I would have never thought about making anything like this.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.