[CollectionService] Make Part turn neon using functions

Hello there,
I just recently started to actually get into scripting, and I was just tinkering around with functions.

But I can’t seem to figure how to make a part to into neon with a function, using collectionService with the script being in ServerScriptService.

game:GetService("CollectionService")
local Bulb = game.CollectionService:GetTagged("Bulb")

function TurnOnAtNight()
	Bulb.Enum.Material = Enum.Material.Neon
end

task.wait()
TurnOnAtNight()


I’m not exactly sure how I can resolve this issue, but I know that this means the game can’t find the Materials?

I have the part tagged as “Bulb”, and output doesn’t show anything. It’s probably just a starter mistake, but hey that’s how I can learn :’)

Well, there are two things I notice you doing wrong. First one is that you are getting one of the tagged objects from the many wrong. You need to loop through each one of them and set their material:

for i, tagged in ipairs(CollectionService:GetTagged("Bulb") do
    tagged.Material = Enum.Material.Neon
end

:GetTagged() function returns a table (more specifically an array) containing all the tagged objects.

Second is that you were setting the bulb’s material incorrectly. I fixed that for you as well. Enums can only be set not read. You can read enums and print them out, yes, but not the way you did.

1 Like

With “tagged” do you mean as in the name of the tag I tagged the part with?

OH YES!

It works!
I just had to replace “tagged” with “Bulb”, added another bracket at the end of GetTagged("Bulb)

Woohoo!
Thanks for telling me how to properly grab tags :smile:

1 Like

for i, v in pairs() do (or for i, v in ipairs() do) loop return two values, the index which means the count (i) and the instance (v).

So if I would loop through 3 parts, the first iteration would return 1 as the count (i) and if I would be at the last iteration which is 3, it would return 3 as the count (i) value.

You can name the i and v whatever you want. Most scripters just leave i as an underscore (_).

1 Like

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