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

How do I stop and start a script?

I'm trying to make an elevator work when I click a button. The only way it should work is if the player is touching the platform of the elevator while clicking the button. So if the player clicks the button without touching the platform then the elevator should not work. I'm having an issue with the touched event constantly being active. When I run the game the button works as it should. Afterwards the button works every time even though I may not be on the platform. Is there some sort of way to reset the touched event so that it turns on or off?

Here is my code:


local function onTouched()
		
	if not debounce then
		
			debounce = true
			
				clicker1.MouseClick:Connect(function(player)
				local Character = player.Character
				Character:SetPrimaryPartCFrame(teleport2.CFrame)
		end)
		
			wait(2)
		
		debounce = false
		
	end
	
end
Elevator.Touched:Connect(onTouched)

2 answers

you can use ~~~~~~~~~~~~~~~~~ script.Disabled = true

 to disable the script and then the opposite to enable it 

Your Touched Event is continuously running because it is being touched by another part. You didn't check if a player is touching it.

local function onTouched()

local humanoid = script.Parent:FindFirstChild("Humanoid")

    if humanoid and not debounce then
            debounce = true

                clicker1.MouseClick:Connect(function(player)
                local Character = player.Character
                Character:SetPrimaryPartCFrame(teleport2.CFrame)
        end)

            wait(2)

        debounce = false

    end

end
Elevator.Touched:Connect(onTouched)

I'm not quite sure this work but try it

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