A button that toggles enabled on a script

Hello. I am currently trying to figure out why the script below does not work. I am trying to disable and re-enable a script by pressing a button. I does enable the script (It starts out disabled) but when I click the button, it does not enable it again. It’s a local script, and I know it has the ability to disable/enable another local script. Could someone please inform me on what I am doing wrong? Thank you for your time

Edit: forgot to mention something

local SpacesCountScript = script.Parent.Parent.Parent.FindFirstChild("IncludeSpaces")
local Button1 = script.Parent

Button1.MouseButton1Click:Connect(function()
SpacesCountScript.Enabled = not SpacesCountScript.Enabled

end)

you need .Enabled! lua will still assert truthiness, add --!strict to the top of your scripts and it will help find errors like this. ah edited

SpacesCountScript.Enabled = not SpacesCountScript.Enabled

Could you show the other script too?

1 Like

Oh, sorry, that is in my script. I just edited it, sorry. I just typed in because it is so short. Forgot to add that here

well I know the other script works, but sure

local TextBox = script.Parent.Type
local TextLabel = script.Parent.Visualiser

TextBox:GetPropertyChangedSignal("Text"):Connect(function()
	local Text = TextBox.Text
	local Letters = string.len(Text)
	TextLabel.Text = Letters .. " character(s) long"
end)

Based on the code you posted, there are a couple of issues with it. Firstly, in the second script, there is an end statement at the end of the second line inside the function, which is causing the function to prematurely terminate. To fix this, you should move the end statement to the end of the function, after the line that sets the text of the TextLabel .

Secondly, the line TextBox:GetPropertyChangedSignal("Text"):Connect(function() is connecting a signal to the TextBox property changed event, but it does not seem to be updating the text of the TextLabel . To update the text of the TextLabel based on the text in the TextBox , you should add the code that sets the text of the TextLabel inside the function that is connected to the TextBox property changed event.

Here is the corrected code:

local SpacesCountScript = script.Parent.Parent.Parent:FindFirstChild("IncludeSpaces")
local Button1 = script.Parent

Button1.MouseButton1Click:Connect(function()
  SpacesCountScript.Enabled = not SpacesCountScript.Enabled
end)

local TextBox = script.Parent.Type
local TextLabel = script.Parent.Visualiser

TextBox.TextBox:GetPropertyChangedSignal("Text"):Connect(function()
  local Text = TextBox.Text
  local Letters = string.len(Text)
  TextLabel.Text = Letters .. " character(s) long"
end)
1 Like

yet again, I should ALWAYS copy the code. It got messed up just because I typed in out, yet again, I fixed it, sorry to make you type a book

lmao no worries, happy to help

1 Like

alright, after changing all the paths correctly, exact same thing. It enables it, but after I click again it doesnt disable

If you are trying to toggle the Enabled property of a script, but it is not being updated. One possible way to do it is to check if the Enabled property is set to true or false , and toggle it accordingly, like this:

local SpacesCountScript = script.Parent.Parent.Parent:FindFirstChild("IncludeSpaces")
local Button1 = script.Parent

Button1.MouseButton1Click:Connect(function()
	if SpacesCountScript.Enabled then
		SpacesCountScript.Enabled = false
	else
		SpacesCountScript.Enabled = true
	end
end)

This way, the Enabled property is set to the opposite of its current value each time the button is clicked.

I’ll test this later, thank you.

30 min))))))))))))))))))))))))))))))

1 Like

alright, I dont even know what to do next. This script does not even enable it. I get no errors, I also checked for pathing problems, nothing. I dont even understand why it is not working. I guess ill keep looking for answers

Maybe the other script has the problem? I doubt it but you could look at it

alright i am actually doing it different. I am going to be checking if the transparency of something is 1, and if it is, the script will disable

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.