Need help with a toggle script

Hello, I am making a toggle script that is supposed to make my other script Enabled/Disabled depending on if the setting is turned on/off, however, for some reason, my script stays disabled after you turn it the setting off

local onoff = true
local ts = game:GetService("TweenService")
local ti = TweenInfo.new(0.5, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut, 0)
local bar = script.Parent.BarBackground.Bar

script.Parent.BarBackground.Button.MouseButton1Click:Connect(function()
	if onoff == true then
		onoff = false
		
		script.Parent.PlayerHider.Enabled = true
		
		ts:Create(bar,ti, {BackgroundColor3 = Color3.fromRGB(163, 68 , 68)}):Play()
		bar.TextLabel.Text = "Off"
		
		bar:TweenPosition(UDim2.new(0.5,0,0.5,0), "InOut", "Quad", 0.5, true)
	else
		onoff = true
		
		script.Parent.PlayerHider.Enabled = false
		
		ts:Create(bar,ti, {BackgroundColor3 = Color3.fromRGB(195, 226, 197)}):Play()
		bar.TextLabel.Text = "On"
		
		bar:TweenPosition(UDim2.new(0,0,0.5,0), "InOut", "Quad", 0.5, true)
	end
end)

basically if I explained it badly, once I try to click the button while the text says On, it’ll say it has turned Off but the script is still enabled. Please Help

Video of my problem:

PlayerHider script:

local Players = game:GetService("Players")

local LocalPlayer = Players.LocalPlayer

local function SetCharacterTransparency(character)
	for i, descendant in ipairs(character:GetDescendants()) do
		if not descendant:IsA("BasePart") then
			continue
		end

		descendant.Transparency = 1
	end
end

for i, player in ipairs(Players:GetPlayers()) do
	if player == LocalPlayer then
		continue
	end

	player.CharacterAdded:Connect(function(character)
		if not player:HasAppearanceLoaded() then
			player.CharacterAppearanceLoaded:Wait()
		end

		SetCharacterTransparency(character)
	end)

	if not player.Character then
		continue
	end

	SetCharacterTransparency(player.Character)
end

Any Help is appreciated, thank you!

What happens if the other player resets? I think the problem is that you are never making the player visible again after toggling it, so currently invisible people will stay invisible until they die

Ya, I thought it would work when I just changed the scripts enabled property back to true but it didnt so I am kind of lost, do I have to change that in the PlayerHider script?

I am not sure how disabling and enabling scripts works, but what about instead of disabling, you use an attribute/boolvalue to check if the setting is enabled or not?

1 Like

Thank you for the suggestion! It worked.

1 Like

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