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

(solved) How do I make a colour changing surface/spotlight?

Hi there! Im currently in the process of making a game and I need a script that allows me to click a button which changes the colour of a light that the button is assigned to. I am a bit confused as to what I need to do to make it work because i have tried altering my code multiple times and every time it hasnt worked. I would really appreciate some help because I have tried everything I can and I have not been able to get it to work.

Here is the code I have so far

local clickdetector = script.Parent:WaitForChild(“ClickDetector”)

clickdetector.MouseClick:Connect(function(player)
    game.Workspace.pinkLight.Surfacelight.Color = BrickColor.new("Pink").Color
end)

(pinkLight is the name of the light i want to change the color of)

2 answers

Put the script and click detector in the part. Also lights use Color3, not brick color.

local clickdetector = script.Parent:WaitForChild(“ClickDetector”)

clickdetector.MouseClick:Connect(function(player)
    script.Parent.SurfaceLight.Color = Color3.fromRGB(255, 170, 255)
end)

Edit:

local clickdetector = script.Parent:WaitForChild(“ClickDetector”)
local part = workspace:WaitForChild("pinkLight") --Waits for the part to load

clickdetector.MouseClick:Connect(function(player)
   part.SurfaceLight.Color = Color3.fromRGB(255, 170, 255)
end)

10 comments

0

Hi! Thanks a lot for you help. So would i put this in the button and then link that to a remote event? Also would this work if i wanted to control the light when the button is a different block?

0

Oh you can put this in a normal script. If you wanted to control the light remotely, I'll edit my answer.

0

Yeah thats what i want it to do so that would be very helpful :)

0

Done! If you have any further questions, do ask.

0

Thank you so much for your help!

0

No problem :)

0

Alright, i still cant get it to work. Do each parts of the script go in different scripts? Like for example the first part would go in the buttons script and the second part would go in the lights script?

0

No, keep all code in one script.

0

Hi again. I still cant get it to work. Would it be possible to contact on discord so i can show you what im doing in more detail so you can show me where im going wrong?

0

Sure. Incompetence#8077

This would work so it changes every second:

while true do
	game.Workspace.pinkLight.Surfacelight.Color = BrickColor.Random().Color
	wait(1)
end

BrickColor.Random().Color converts a random BrickColor to Color3 since lights use Color3 instead of BrickColor.

If you want to make it change every time you click, you'd do this:

local clickdetector = script.Parent:WaitForChild(“ClickDetector”)
	 
clickdetector.MouseClick:Connect(function(player)
	game.Workspace.pinkLight.Surfacelight.Color = BrickColor.Random().Color
end)

If you want to make it pink when you click it, follow Utter_Incompetence's answer.

I hope this helps you.

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