I'm trying to create a brick that when touched, creates a surface light instance with brightness 1000, and also changes script.Parent's brick material to Neon. One the touch has ended, I want it to make the material plastic once again and :destroy() the surfacelight element. I've tried using while do loops, and also if else. The problem is the touch is an event, not a boolean, and therefore these methods won't work. Is there any simple solution or alternative? One code I've tried included:
script.Parent.Touched:connect(function()
local light = Instance.new("SurfaceLight", game.Workspace.Part);
light.Brightness = 1000
script.Parent.Material = "Neon"
end)
I am able to make a script that creates a light on the block for a set time and then shut off until touched again, but I want one that stays on until the touch has ended. My idea is to use something like:
if script.Parent.TouchEnded == True
But the problem is that TouchEnded is not a boolean. Thanks to any respondents.
Edit 1: I've actually solved the problem with a few simple adjustments. My new code is as follows:
script.Parent.Touched:connect(function()
local light = Instance.new("SurfaceLight", game.Workspace.Part);
light.Brightness = 1000
script.Parent.Material = "Neon"
script.Parent.TouchEnded:connect(function()
script.Parent.SurfaceLight:Destroy()
script.Parent.Material = "Plastic"
end)
end)
One last concern though, is that every time the part is touched by my character or if I stop touching it, there is a slight lag spike. Is there a way to optimize this?
Edit 2: I've solved the problems with this script:
script.Parent.Touched:connect(function()
script.Parent.SurfaceLight.Brightness = 1000
script.Parent.Material = "Neon"
script.Parent.TouchEnded:connect(function()
script.Parent.SurfaceLight.Brightness = -1000
script.Parent.Material = "Plastic"
end)
end)

1 comment
Try placing a wait() or two. That's what I usually do if a script is causing slight lag