Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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.

4 comments

0

why just not copy the part?

0

there are more than 1 parts named gold. I don't want a million scripts...

0

im not sure but try GetChildren("gold") in place of waitforchild

0

:GetChildren() returns a array

1 answer

✓ Accepted answer

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

4 comments

0

Just as a safety measure, I would lower the string first.

0

eh

0

ill edit my answer then'

0

Thank you

This discussion is preserved as it was. New answers, comments, and votes are closed.