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

I'm trying to connect a TextButton to a light, why doesn't this script work?

Hello! I'm trying to make a game where you click a TextButton and a flashlight is supposed to flash temporarily, then be ready to use again. I currently have this code and I'm not sure why it won't work?

local button = game.StarterGui.ScreenGui.TextButton
local flashlight = game.Workspace.Light
local flash = flashlight.PointLight

local function flash()
	flash.Enabled = true
	wait(0.1)
	flash.Brightness = 0.9
	wait(0.1)
	flash.Brightness = 0.8
	wait(0.1)
	flash.Brightness = 0.7
	wait(0.1)
	flash.Brightness = 0.6
	wait(0.1)
	flash.Brightness = 0.5
	wait(0.1)
	flash.Brightness = 0.4
	wait(0.1)
	flash.Brightness = 0.3
	wait(0.1)
	flash.Brightness = 0.2
	wait(0.1)
	flash.Brightness = 0.1
	wait(0.1)
	flash.Brightness = 0
	wait(0.1)
	flash.Enabled = false
	flash.Brightness = 1
end

button.MouseButton1Click:Connect(flash)

1 answer

✓ Accepted answer

You are accessing StarterGui, no, do not do that, you need to access PlayerGui instead. Best way to do that is just putting the script inside of the button and setting the button to script.Parent. Also better use :WaitForChild() on variables as they may not load when the script is executed so:

local button = script.Parent
	local flashlight = game.Workspace:WaitForChild("Light")
	local flash = flashlight:WaitForChild("PointLight")
	 
	local function flash()
	    flash.Enabled = true
	    wait(0.1)
	    flash.Brightness = 0.9
	    wait(0.1)
	    flash.Brightness = 0.8
	    wait(0.1)
	    flash.Brightness = 0.7
	    wait(0.1)
	    flash.Brightness = 0.6
	    wait(0.1)
	    flash.Brightness = 0.5
	    wait(0.1)
	    flash.Brightness = 0.4
	    wait(0.1)
	    flash.Brightness = 0.3
	    wait(0.1)
	    flash.Brightness = 0.2
	    wait(0.1)
	    flash.Brightness = 0.1
	    wait(0.1)
	    flash.Brightness = 0
	    wait(0.1)
	    flash.Enabled = false
	    flash.Brightness = 1
	end
	 
	button.MouseButton1Click:Connect(flash)

Also just a tip, instead of writing the same line of code 10 times use (for loops).

Hope this helps.

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