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

How can I make this script find all parts with a certain name and make them strobe?

Asked by 4 years ago

So basically, my little cousin is super into Fire Alarms, and I thought I would script a Fire Alarm set for him so he can use it in his games. I'm having trouble with an in pairs loop and getting it to find all parts in a model named "Strobe", and making them do as expected, strobe (Namely changing the material to neon and making a point light brighter then reversing the changes to make a bright flashing light).

Anyways, here is the code. Ignore the giant clunky loop, I'm too lazy to make it more efficient (even though that may be part of the problem):

-- {{ Setting Variables }} --

local h = script.Parent.Parent
local c = script.Parent
local f = workspace.AlarmSet.Strobe
local a = workspace.AlarmSet
local s = workspace.AlarmSound
local p = f.PointLight

-- {{ Main Strobe Function }} --

c.MouseClick:Connect(function() --Linking a function to the click of the handle
    s:Play()
    local parts = a:GetChildren()
    for i,v in pairs(parts) do
        if v.name == "Strobe" then
            while true do                --Setting up a giant loop to cycle through 
                v.Material = "Neon"
                v.PointLight.Brightness = 5
                wait(0.05)
                v.Material = "Neon"
                v.PointLight.Brightness = 5
                wait(0.05)
                v.Material = "Neon"
                v.PointLight.Brightness = 5
                wait(0.05)
                v.Material = "Neon"
                v.PointLight.Brightness = 5
                wait(0.05)
                v.Material = "Neon"
                v.PointLight.Brightness = 5
                wait(0.05)
                v.Material = "Neon"
                v.PointLight.Brightness = 5
                wait(0.8)
                v.Material = "Neon"
                v.PointLight.Brightness = 5
                wait(0.05)
                v.Material = "Neon"
                v.PointLight.Brightness = 5
                wait(0.05)
                v.Material = "Neon"
                v.PointLight.Brightness = 5
                wait(0.05)
                v.Material = "Neon"
                v.PointLight.Brightness = 5
                wait(0.05)
                v.Material = "Neon"
                v.PointLight.Brightness = 5
                wait(0.05)
                v.Material = "Neon"
                v.PointLight.Brightness = 5
                wait(0.8)
                v.Material = "Neon"
                v.PointLight.Brightness = 5
                wait(0.05)
                v.Material = "Neon"
                v.PointLight.Brightness = 5
                wait(0.05)
                v.Material = "Neon"
                v.PointLight.Brightness = 5
                wait(0.05)
                v.Material = "Neon"
                v.PointLight.Brightness = 5
                wait(0.05)
                v.Material = "Neon"
                v.PointLight.Brightness = 5
                wait(0.05)
                v.Material = "Neon"
                v.PointLight.Brightness = 5
                wait(1.6)
            end         
        end
    end
end)

Also the comments are to help him with coding in the future, let me know if they are stupid lol

1 answer

Log in to vote
1
Answered by
Fifkee 2017 Community Moderator Moderation Voter
4 years ago
Edited 4 years ago

GOODNESS GOLY GOSH. First off, that would never get to the next part. I recommend you set up a coroutine or a spawn to contain that loop.

Also, you're never changing the brightness/colour of the pointlight within it. You can also use x and y or z to check if x isn't nil/false and return y, otherwise return z.

Example:

local z = true and 'yes' or 'no' --//returns yes local z = false and 'yes' or 'no' --//since one side of the "and" conditional fails, it returns no. cool!

Now, with using spawn.

Spawn allows you to create a seperate thread to execute your code in, like so:


frequency = 0.3 local rainbow = function (i) local r = math.sin(frequency*i + 0) * 127 + 128; local g = math.sin(frequency*i + (2*math.pi/3)) * 127 + 128; local b = math.sin(frequency*i + (4*math.pi/3)) * 127 + 128; return Color3.fromRGB(r, g, b) end for i, v in pairs(workspace:GetDescendants()) do if (v:IsA('BasePart')) then spawn(function(a) local inc = 1; while game:GetService('RenderStepped'):Wait() do v.Color = rainbow(i) inc = inc+1; end end) end end

Without the spawn, you would never get to the second iteration in the pairs because your code would be too busy lingering over the while loop. Now, you'll have multiple threads running your epic cool rainbow code. Awesome!

Read more on coroutines and spawn at https://scriptinghelpers.org/questions/83263/how-would-i-skip-over-a-wait-before-it-has-ended#77392.

Now that you're informed on both, you can shorten your loop to something like this.

 for i,v in pairs(parts) do
        if v.name == "Strobe" then
    spawn(function(f)
            while true do                --Setting up a giant loop to cycle through 
                      v.Material = "Neon"
                      v.PointLight.Brightness = v.PointLight.Brightness == 0 and 5 or 0;
                      wait()
                end         
            end
    end)
end
1
holy crap that indentation is bad. why do people still turn tabs into spaces? maaaan.. Fifkee 2017 — 4y
0
LMAO that intro got me I'll let you know if it works MustangHeart 67 — 4y
0
Awesome! It works great. Thanks for the help, I'll be sure to look into spawn and coroutines in the future (although they seem to be a bit advanced for me). MustangHeart 67 — 4y
Ad

Answer this question