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

Do something when the part's name is 'gold'?

Asked by 5 years ago
Edited 5 years ago
local gold = workspace:WaitForChild('gold')

gold.Touched:connect(function(hit)
    if hit.Parent:FindFirstChild('Humanoid') then
    print('hi')
    end
end)

i need help making it so if any part named 'gold' get touched, it prints hi and not just that one part, which is what its doing.

0
why just not copy the part? tacotown2 119 — 5y
0
there are more than 1 parts named gold. I don't want a million scripts... fuffieboy 17 — 5y
0
im not sure but try GetChildren("gold") in place of waitforchild tacotown2 119 — 5y
0
:GetChildren() returns a array User#23365 30 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

you can use a generic for loop which literates through a table, in which :GetChildren() returns.

local gold = workspace:GetChildren() -- returns a table or a array

for _,v in pairs(gold) do -- v is the value its currently looping over
    local lower = string.lower(v.Name)

    if lower == "gold" then -- checks if the name is "gold"
        v.Touched:Connect(function(hit)
            if hit.Parent:FindFirstChild("Humanoid") then
                print("hi")
            end
        end)
    end
end
0
Just as a safety measure, I would lower the string first. green271 635 — 5y
0
eh User#23365 30 — 5y
0
ill edit my answer then' User#23365 30 — 5y
0
Thank you fuffieboy 17 — 5y
Ad

Answer this question