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

How to create a while touched script?

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

0

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

1 answer

script.Parent.Touched:connect(function()
    local light = Instance.new("SurfaceLight", game.Workspace.Part)
    light.Name = "Light"
    light.Brightness = 1000
    script.Parent.Material = "Neon"
end)
script.Parent.TouchEnded:connect(function() -- TouchEnded is a function
	game.Workspace.Part.Light:Remove()
script.Parent.Material = "Plastic"
end)

Hope this helped.

1 comment

0

That script does not work for me, although I understand your concept and was able to create a working and non laggy code. Thank you anyways!

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