I cannot figure out how to use tables with instances, and compare them to them by string( their name ),

  1. What is the issue? / What do you want to achieve? I have no clue how to get objects from a table, as in, if v ( in pairs(table) )
    equals the name I ask for of an object I want to clone from that table.

  2. What solutions have you tried so far?
    I’ve tried meta tables, only to find out indexing them is quite convoluted ( that plus the definition of a meta table isn’t very clear to me ), Id assume its this:

local table = {
[name1] = {Name = name,Object = object};
[name2] = {Name = name, Object = object}
}

--But atlas I have no clue how to index this with for i, v in pairs(table),

--Ive tried a vanilla table:
local table = {
1, 
2
}



--I can expand on what I'm trying to achieve here:


--Imagine you have a character, who is a human-half-pirate ship,
--And I wanted to add GUNS to their broadsides ( This part by the way, I got functional, I have each slot for --each cannon accounted for by values, and I HAVE successfully spawned one TYPE of gun from a directory I manually set )
--But upon making a library for every gun type there is, I cannot seem to figure out how to use in pairs() in this context.

--Let me show everyone the current method I have with acouple pictures:
1 Like

image

Let me break down this image:

Val is the deck index, or the cannon slot the player picked to place a cannon at.
Val2 is a string, its the name of the cannon type they want.
Player is simply called because this is directed to their player.Character which is defined above and not shown here in the image.

Under it is the table I wanted to use, which by the way, works, it recognizes it as a table.
But upon trying to get an object from this table, which, here is the method I used here:

I made a funciton stating yeah, I’m associating the name of the instance I want, you pull the instance from this table of gun types,
If what the player asks for is in the table, clone that object.
image

1 Like

maybe try tostring(currentgun)

1 Like

Current gun is a string,
v.name is the name of the instance I indexed from my table.

So although I tried what you asked with the possibility it’d work, predictably it did not.
But this should give you more context on how it works.

1 Like

gunList:GetChildren() might be what you need here:

for i, v in pairs(gunList:GetChildren()) do

Unless I’ve misinterpreted your post; it’s a bit confusing imo like the way you worded it is confusing to me if you could help explain it again then I might be able to help you

1 Like

gunlist:GetChildren() has already been done above, but Ill try to move it here since I suppose it makes things more compact.

Let me reexplain how this all works:
Player asks for the type of gun they want, represented as Val2 ( a string ),
Player tells the script where they wanna place that gun on their ship, represented as Val ( A number ).

So lets say, I wanted a Long-gun on slot 1 of my ship.
The script compares the name I asked for, to the list of names in this table:
image

How would I compare the name ( Val2 ) to the instances in this table to preform a :clone() ?

Heres what happens when I try this out, it thinks Im asking to clone a string, when, in reality Im using the name of what Im asking for to pull from a table of instances which can be cloned.
image

2 Likes

Maybe something like this:

local function Associate(gunList: Instance, Name: string)
     print(currentgun)
     for i, child in pairs(gunList:GetChildren()) do
           if child.Name == currentgun then
                print("true")
                if Ono ~= nil then
                    local rearm = child:Clone()
                end
           end
     end
end

had to retype your script because yours isn’t contained in a formatted text container (and I shortened some of your code)

basically, currentgun is a string and you cannot call the :Clone method on a string since that is a method that is inherited from the Instance class which strings aren’t

1 Like

Doesn’t seem to work, also, currentgun is just Val2 but I guess “rebranded”

I’ll simply post what I’ve done with this information here, nothing is in output by the way, no errors.
But the action of actually adding that cannon from the list just doesnt seem to happen.

Also, I changed “child” to v because it is the instance I’m trying to get from the table, I had also removed acouple things from my original script to make it a tad more compact.

--!strict
--General Locals--
local event = game.ReplicatedStorage.AddArma
local player = game:GetService("Players").PlayerAdded:Wait() or game.Players:GetPlayerFromCharacter()
local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
local Gui = player.PlayerGui:WaitForChild("CustomizationGUI")
local Selection = Gui.Frame.DeckIndex:WaitForChild("Selectionor")
local TextBox = Gui.Frame.DeckIndex
local Saved = script.Value
local cooldown = false
--General Locals--








--spawning gun--
local toevent = event.OnServerEvent:Connect(function(player, Val, Val2)

	--Defining gun-type--
	local currentgun = script.CannonType.Value
	local gunlist = game.ReplicatedStorage.Cannons
	currentgun = Val2 -- Gun Type
	--Defining gun-type--








Saved.Value = Val -- Index Slot
print(Saved.Value)
	local Stern = player.Character:FindFirstChild("Stern")
	repeat task.wait() until Stern:GetDescendants() ~= nil
	local Forecastle = player.Character:FindFirstChild("Forecastle")
	repeat task.wait() until Forecastle:GetDescendants() ~= nil

		local Ono = Stern:GetChildren()
		local Dos = Forecastle:GetChildren()
		print(Ono, Dos) -- Index Cache
	local function Associate(gunlist: Instance, Name: string)
		for i, v in pairs(gunlist:GetChildren()) do
			if v.Name == currentgun then
			local gun = v
			if Ono ~= nil then do
				local rearm = gun:Clone()
				local attach : Attachment = Stern:FindFirstChild("r"..tostring(Saved.Value))
				local gunatt = Instance.new("Attachment")
				rearm.Parent = Stern
				gunatt.Parent = rearm:FindFirstChild("OBJ")

					local constraint = Instance.new("RigidConstraint")
					constraint.Attachment0 = gunatt
					constraint.Attachment1 = attach
					constraint.Parent = Stern
				end

					if Dos ~= nil then do
					print("cloned")
					local rearm = gun:clone()
					local attach : Attachment = Forecastle:FindFirstChild("r"..tostring(Saved.Value))
					local gunatt = Instance.new("Attachment")
					rearm.Parent = Forecastle
					gunatt.Parent = rearm.OBJ

					local constraint = Instance.new("RigidConstraint")
					constraint.Attachment0 = gunatt
					constraint.Attachment1 = attach
					constraint.Parent = Forecastle
					end
						end
				end
			end
		end
	end	
end)
--spawning gun--```
1 Like

Wait why don’t you just do gunlist:FindFirstChild(currentgun)?

Like:

if gunlist:FindFirstChild(currentgun) then

I fixed it, apparently I didnt need to do that “associate” function, what you had stated about :Clone() not working on strings seemed to fix the issue, with some adjustments.

Im gonna try your final comment out as it seems to make it even more compact,
I don’t think this will work because again, current gun is a string, not an object.

1 Like


Yeah no it didnt work for exactly the reason I mentioned,
Thank you for helping me out, I’m going to solution-check your relevent text.

1 Like

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