I’m trying to make a system where when you select two attachments and press a button, a rod constraint appears, with both ends connecting to an attachment.
Now, I’ve already made the selection system, and I can put the selected attachments into a table. From here on out, I don’t know how to continue…
--First thing's first, I'd need to know if I have exactly two attachments selected when I press my button
button.MouseButton1Click:Connect(function()
if #selectedAttachmentArray == 2 then
-- next, I'd have to check if there's already a rod there, meaning I'd need to store it. This is where I'm stuck at...
-- if there isn't a rod, create one, and somehow store it...
end
end)
Thanks!
I’m not really sure what you are trying to do. Look it up later? Datastore? If not datastore, then just do something like:
local lookup = {}
local function exists(attach1, attach0): boolean
return lookup[attach1] and lookup[attach1][attach0] or lookup[attach0] and lookup[attach0][attach1]
end
local function connect(attach1, attach0)
lookup[attach1] = lookup[attach1] or {}
lookup[attach1][attach0] = true
end
I wouldn’t use something like this but it should work. I would probably use and id system instead of indexing by instance.
yes, exactly, a lookup table. (first time hearing about it, but it sounds about right)
If you can, may you explain how it works? Currently, I’m not understanding. Thanks!
what about an ID system? How would that work?
Well, you would just call connect with array[1] and array[2], then clear the array. If you want to see if any two attachments are connected, you would just check if exists(attach1, attach2)
. With an ID system, instead of indexing by attachment instance, I would give each attachment some unique ID, probably just integers starting at one, then each rod would be a pair of any of those IDs.