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

Why isnt Strobe setting for my flashlight working?

I am trying to make a flashlight with a strobe setting but it's not working.

I want the light to turn on when the player clicks. When the player clicks again it needs to switch to strobe. When the Player clicks again it needs to set the bool value "Strobe" to false and set the spotlight's enabled property to false

When I click the light turns on and Strobe is set to true. When I click again nothing happens. When I click a third time again nothing happens. If all had gone as planned the second click would have made the light strobe and the third click would have turned it all off ready to be started over with the next click.

script.Parent.Light.SpotLight.Enabled = false
script.Parent.LightSettings.Strobe.Value = false
script.Parent.Equipped:connect(function(mouse)
	mouse.Button1Down:connect(function()
		if script.Parent.LightSettings.Strobe.Value == false and script.Parent.Light.SpotLight.Enabled == false then
			script.Parent.Light.SpotLight.Enabled = true
			script.Parent.LightSettings.Strobe.Value = true
		else if script.Parent.LightSettings.Strobe.Value == true and script.Parent.Light.SpotLight.Enabled == true then
			while script.Parent.LightSettings.Strobe.Value ==true do
			script.Parent.Light.SpotLight.Enabled = true
			wait(0.2)
			script.Parent.Light.SpotLight.Enabled = false
			end
		else if script.Parent.LightSettings.Strobe.Value == true and script.Parent.Light.SpotLight.Enabled == true then
			script.Parent.LightSettings.Strobe.Value = false
			script.Parent.Light.SpotLight.Enabled = false
		end
		end
		end
	end)
end)

Thanks for your help.

2 answers

✓ Accepted answer

First we shall remember the Spotlight and Strobe are false by default.

Here are your if statements, which are the main problem. I'm going to substitute your long paths for variables, so everything's shorter. I'm also going to change your else if to elseif. This lets use only need a single end, as well as helping prevent some different errors.

if strobe.Value == false and spotLight.Enabled == false then
	spotLight.Enabled = true
	strobe.Value = true
elseif strobe.Value == true and spotLight.Enabled == true then
	while strobe.Value == true do
		spotLight.Enabled = true
		wait(0.2)
		spotLight.Enabled = false
	end
elseif strobe.Value == true and spotLight.Enabled == true then
	strobe.Value = false
	spotLight.Enabled = false
end

Ah! So much neater! It still won't work, but now the problem will be easier to solve.

So let's go through this step-by-step, like a computer would.


Click 1:

  • strobe is false
  • spotLight is false

Therefore, if #1 will pass.

  • spotLight is set to true
  • strobe is set to true

Click 2:

  • spotlight is true
  • strobe is true

Therefore, elseif#1 will pass.

  • The loop will continue until strobe is false.

Wait wait wait, when will strobe be false? Never. It's only set to false under the second elseif, which will never run. Elseif#2 has the exact same conditions as elseif#1. The second will only be checked if the first doesn't pass. But if the first doesn't pass, neither will the second. Therefore the second elseif will never run.

But back to the while loop. Although it will never stop, we know it's running, so why isn't the light flashing? Take another look.

while strobe.Value == true do
	spotLight.Enabled = true
	wait(0.2)
	spotLight.Enabled = false
end

Well, the light is flashing. But it's so fast you can't see it.

When a loop reaches the bottom, it immediately goes back up to the top. Therefore as soon as the light is set to false, it will be set to true again super fast. The following is totally equivalent to what you wrote, just arranged a little differently:

while strobe.Value == true do
	wait(0.2)
	spotLight.Enabled = false
	spotLight.Enabled = true
end

To fix this you just need another wait:

while strobe.Value == true do
	spotLight.Enabled = true
	wait(0.2)
	spotLight.Enabled = false
	wait(0.2)
end


So how do you fix this? Since you have three different actions happening, I would recommend making a variable equal to a number. It will be either 1, 2, or 3.

If it equals 1 you'll do a, if it's 2 you'll do b, if it's 3 you'll do c and set the number back to 1.

if x == 1 then
	--do this
elseif x == 2 then
	--do this
elseif x == 3 then
	--do this
end

But since this requires basically entirely new code, I'll leave you to try it out.

Thanks Perci1 you really helped me. The code is neater, works very good, and I learned something!

I didn't think there was a difference between else if and elseif. I didn't think about using a variable in that way, I've always put a bool value into the model/tool and used that.

Here's the complete code:

local x = 1
local strobe = script.Parent.LightSettings.Strobe
local light = script.Parent.Light.SpotLight

script.Parent.Equipped:connect(function(mouse)
	mouse.Button1Down:connect(function()
		if x == 1 then
			light.Enabled = true
			x = 2
		elseif x == 2 then
			strobe.Value = true
			x = 3
			while strobe.Value == true do
				wait(0.2)
				light.Enabled = true
				wait(0.2)
				light.Enabled = false
			end
		elseif x == 3 then
			strobe.Value = false
			light.Enabled = false
			x = 1
		end
	end)
end)

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