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

how do i make a toggleable players invisible switch?

make other players invisible with a switch. this script does not work. why?

local button = script.Parent
local on = false

button.MouseButton1Click:Connect(function()
	if on == true then
		on = false
		button:TweenPosition(UDim2.new(0.249, 0,0.5, 0),"Out","Quad",0.2,true)
	elseif on == false then
		on = true
		button:TweenPosition(UDim2.new(0.800, 0,0.5, 0),"Out","Quad",0.2,true)
	end
end)

if on == true then
	--invis
	
	local Players = game:GetService("Players")
	
	Players.PlayerAdded:Connect(function(Player)
		Player.CharacterAdded:Connect(function(Character)
			for _, Player in pairs(game.Players:GetPlayers()) do
				local Character = Player.Character
				if (Player.Character ~= Character) then
					local CharacterStuff = Character:GetDescendants()
					for _, BodyPart in pairs(CharacterStuff) do
						if (BodyPart:IsA("BasePart")) then
							BodyPart.Transparency = 1
						end
					end
				end
			end
		end)
	end)
	
end

1 answer

✓ Accepted answer

The script ain't gonna run the if statement again. So since variable on is false by default, every thing in that if statement code block (line 14 to line 35) did not run. I suggest moving the if statement under the characteradded event so just in case people reset.

Sample:

    --invis

    local Players = game:GetService("Players")

    Players.PlayerAdded:Connect(function(Player)
        Player.CharacterAdded:Connect(function(Character)
if on == true then
            for _, Player in pairs(game.Players:GetPlayers()) do
                local Character = Player.Character
                if (Player.Character ~= Character) then
                    local CharacterStuff = Character:GetDescendants()
                    for _, BodyPart in pairs(CharacterStuff) do
                        if (BodyPart:IsA("BasePart")) then
                            BodyPart.Transparency = 1
                        end
                    end
                end
            end
end
        end)
    end)

Script Explaining the concept

local a = 1;
if a==1 then
	print'1'; --> prints 1 because variable a==1
else
	print'2'; --> does not print 2 since a~=1
end;
a = 2;-- >nothing else happens because everything happened on the top.

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