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

Light script not functioning anymore?

Hello, I need help with this script that wont work any more

local light = script.Parent
local lightlight = light.SpotLight
local lv = game.ServerStorage.lightvalue.Value

while true do
wait(.1)	
	if lv == true then
		lightlight = false
	elseif lv == false then
		lightlight.Enabled = true
	end
end

i have another script changing light value and storing it in game.serverstorage.lightvalue.Value (light value is a boolean) Thanks

1 answer

The problem with your code is where you're defining the 'lv' variable, as well as line 8.

Lets talk about while loops. They repeat any code inside the scope of the loop until the given condition is false. So, if your code has repeated waiting a decisecond¹ and checking the 'lv' variable, eventually the variable will not be up to date with the actual value.

You have cached the 'lv' variable to hold the value of lightvalue at the moment that the script reads line 3.

To allow your script to have live feed of the lightvalue's value, you need to define the 'lv' variable inside the while loop.

Line 8: lightlight.Enabled = false; You should have indexed the 'Enabled' property

local light = script.Parent:WaitForChild("SpotLight");

while wait(.1)   do  
	local lv = game.ServerStorage.lightvalue.Value
	lightlight.Enabled = lv
end

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