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

Problems with variables. I can't figure out how to set up this script. Help please?

Trying to make a click detector button for different types of lights. All of the lights are billboard gui's, but I am just trying to enable and disable to shut them off. Also the lights are all individual. I feel like I'm missing a variable. Help please?

``

local isOn = false

function on()
 for i = 1, 18 do
        local light = script.Parent["RedLight" .. i].RedLight.Enabled
isOn = true

end

function off()
for i = 1, 18 do
        local light = script.Parent["RedLight" .. i].RedLight.Enabled
isOn = false
end

function onClicked()
 
 if isOn == true then off() else on() end

end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

on()

1 answer

I'm not completely sure what you're trying to accomplish.. but from the looks of it, you want to toggle a certain something whenever the ClickDetector is clicked...

If yes, you can simply make use of the if-else statement, which would allow you to execute code if a condition is true, or another piece of code if not true (false/nil).

All you would have to do is set up a function to toggle a pre-set boolean value, and using whatever said value for your works.

local status = false
 -- assigning `status` to `false`

local toggle = function()
	status = not status -- toggling boolean value to its opposite.
	-- false would become true and vice versa

	if status then -- if true then
		print(status) -- true 
		for index = 1, 18 do
			local light = script.Parent["RedLight"..index].RedLight.Enabled
		end
	else -- if not true (false/nil) then 
		print(status) -- false
		for index = 1, 18 do
			local light = script.Parent["RedLight"..index].RedLight.Enabled
		end
	end
end

script.Parent.ClickDetector.MouseClick:connect(toggle)

2 comments

0

This is image lights, not stats.

0

I never mentioned "stats".. explain yourself better.. there's no such thing as 'Image lights", so say exactly what instance you are working with.

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