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()
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.
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 (_).