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

How to make this script disable after touching part once?

When a player steps on a part a pointlight that was previously disabled becomes enabled and after 3 seconds it becomes disabled again. The problem is that if they step on it again it keeps happening which is not the purpose. Would love some help.

script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local Block = game.Workspace.Flickeringlightbulb.SmoothBlockModel
Block.PointLight.Enabled = true
wait(3)
Block.PointLight.Enabled = false
end
end)

3 comments

0

What are trying to do exactly? Do you want it to never be able to happen again, or do you want a cooldown?

0

never to happen again

0

There's 2 ways, you can either destroy the part, completely remove it from the game, after they touch it, or disable the script if you want the part still there, after they touch it.

2 answers

There's 2 ways to not let that script run again.

The first method would be to completely destroy the part, and it's children, from the game.

The second method would be to disable the script, if you still want the part to be there.


First Method: Destroy

To do this, you would wait until the part was touched by a person, and then destroy it after you turn off the lights. This will completely remove the part from the game.

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local Block = game.Workspace.Flickeringlightbulb.SmoothBlockModel
		Block.PointLight.Enabled = true
		wait(3)
		Block.PointLight.Enabled = false
		script.Parent:Destroy() --// Destroys the part after being touched and turning off the lights.
	end
end)

Second Method: Disable

To do this, you would wait until the part was touched by a person, and then disable the script so it doesn't run again. It will keep the part in the game.

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local Block = game.Workspace.Flickeringlightbulb.SmoothBlockModel
		Block.PointLight.Enabled = true
		wait(3)
		Block.PointLight.Enabled = false
		script.Disabled = true --// Disables the script so it can't run again, keeps the part in the game.
	end
end)

Hope this helped! If this worked, let me know by selecting this as the answer!

1 comment

0

neither of these are good fixes, someone could repeatly jump on the pad and make it so the script gets disabled and the light won't turn off since the script is disabled/destroyed.

you said you wanted it never to happen again. a way that you could do this is by adding a value to make sure it doesn't turn the pointlight on again, if you are never going to use the script again you could also destroy it.

local wasbuttonused = false
script.Parent.Touched:Connect(function(hit)
	if wasbuttonused ~= true then
		if hit.Parent:FindFirstChild("Humanoid") then
			wasbuttonused = true
			local Block = game.Workspace.Flickeringlightbulb.SmoothBlockModel
			Block.PointLight.Enabled = true
			wait(3)
			Block.PointLight.Enabled = false
		end
	end
end)

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