Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Parent A NameTag In Order To Different Parts?

Asked by 3 years ago

So I Am Trying To Do Parent Like I Have 3 Parts And I Want Is To That When A Player Touches A Part The Thing Gets Parented To The Next Part In Order (IDk How To Explain maybe the script can tell you)

local cs = {script.Parent.c0, script.Parent.c1, script.Parent.c2} -- these are the parts that i want to be nametags parent when they are touched

local tag = script.Parent.NameTag 

print(cs)
0
I DONT WANNA PUT A BILLION SCRIPTS IN EACH PART kidsteve923 139 — 3y
0
I DONT WANNA PUT A BILLION SCRIPTS IN EACH PART kidsteve923 139 — 3y

1 answer

Log in to vote
1
Answered by 3 years ago

if you want to keep from duplicating the same script into multiple parts, you can use a for loop to connect to an event for every part in your table

not completely sure if this will work though

local cs = {script.Parent.c0, script.Parent.c1, script.Parent.c2} -- these are the parts that i want to be nametags parent when they are touched

local tag = script.Parent.NameTag 

for i, part in pairs(cs) do
    part.Touched:Connect(function()
        if table.find(cs, part) then -- make sure the part is in the table when it is touched
            tag.Parent = cs[1] -- set the parent to the next value in the table
            table.remove(cs, 1) -- remove the part from the table so that another part will be next
            if #cs == 0 then -- no values left in the table
                cs = {script.Parent.c0, script.Parent.c1, script.Parent.c2} -- reset the table
            end
        end
    end)
end

print(cs)
Ad

Answer this question