Enable / Disable a script when clicked on part

I am trying to make so when you click a different part the script enables or disables depending on it’s state, the part with the script would be a sensor.

So when enabled, the sensor would work when touched and when disabled the sensor would not work when touched, and it would be controlled from clicking a different part.

I know nothing about scripting therefore I am asking about this, any help would be appreciated.

2 Likes

Script Under Part:

local scriptt = -- game.ServerScriptService.Script
local part = script.Parent

part.Touched:Connect(function()
  if scriptt.Enabled == true then
    scriptt.Enabled = false
  else 
    scriptt.Enabled = true
end)

Insert a click-detector into the part that’s the button, then parent it to a script and write this in the script.

local cd = script.Parent
local scriptToToggle = --script location

cd.MouseClick:Connect(function()
  if scriptToToggle.Enabled then
    scriptToToggle.Enabled = false
  else
    scriptToToggle.Enabled = true
end)
1 Like

Naming the script “scriptt” can be confusing in code. It would be better to give it a name that efficiently communicates what the variable holds.

Experience tells me disabling a script that started running, wont halt the code inside the script.

I’d need to know what is happening inside the script to offer an alternative.

1 Like

I was going to say “Script,” but I can’t or it won’t work. I understand though.

I was wondering the same thing, in that case @duchol666 could create a variable within the script that stores whether the functional part of the script should or shouldn’t run.