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

I am trying to make a toggle invisibility, how would I do so?

It works but there is two problems with it, one, it doesn't make hats and faces invisible too and two, when I toggle back to visible then it adds a grey block around the players torso.

local event = script.InvisFunction
local Val = script.Parent.Hidden.Value

event.OnServerEvent:Connect(function(player)
	if Val == false then
	Val = true
    local character = player.Character or workspace:FindFirstChild(player.Name)
	    for k, v in pairs(character:GetChildren()) do
        if v:IsA('BasePart') then
            v.Transparency = 1
            local decal = v:FindFirstChild('Decal')
            if decal then
                decal.Transparency = 1
            end
        end
		    end
	else
		Val = false
    local character = player.Character or workspace:FindFirstChild(player.Name)
	    for y, t in pairs(character:GetChildren()) do
        if t:IsA('BasePart') then
            t.Transparency = 0
            local decal = t:FindFirstChild('Decal')
            if decal then
                decal.Transparency = 0
            end
        end
	    end
	end
end)

3 comments

0

What kind of toggle invisibility? Making other people invisible? or making every one?

0

A button that you click to make yourself invisible but shows for everyone.

0

Like how some murder mystery games are when they use the ghost ability and it makes them invisible for a certain period of time when they are murderer but make it toggle instead.

1 answer

✓ Accepted answer

I suggest creating a folder inside of ServerStorage to store the player's Accessories.

Also that 'grey block' that is around the player's torso is the HumanoidRootPart. It's default transparency is set to 1, and when you're getting all the parts inside the Character you're also getting the HumanoidRootPart and setting the transparency to 0.

local ss = game:GetService("ServerStorage")
local Event = game.ReplicatedStorage.InvisEvent
local isInvis = false

Event.OnServerEvent:Connect(function(Player)
	
	local Character = Player.Character
	
	if isInvis == true then
		isInvis = false
		
		for _, part in pairs(Character:GetDescendants()) do

			if part:IsA("BasePart") or part:IsA("MeshPart") then
				part.Transparency = 1

			elseif part:IsA("Accessory") or part:IsA("FaceInstance") then

				if not ss.PlayerAccessories:FindFirstChild(Player.Name) then
					local PlayerAccessoryFolder = Instance.new("Folder")
					PlayerAccessoryFolder.Name = Player.Name
					PlayerAccessoryFolder.Parent = ss.PlayerAccessories
				end
				part.Parent = ss.PlayerAccessories[Player.Name]
			end
		end	
	else
		isInvis = true
		
		if ss.PlayerAccessories:FindFirstChild(Player.Name) then
			
			for _, Accessory in pairs(ss.PlayerAccessories[Player.Name]:GetChildren()) do
				if not Accessory:IsA("FaceInstance") then
					Accessory.Parent = Character
				else
					Accessory.Parent = Character.Head
				end
			end
			
			for _, part in pairs(Character:GetDescendants()) do
				if part:IsA("BasePart") or part:IsA("MeshPart") then
					if part.Name ~= "HumanoidRootPart" then
						part.Transparency = 0
					end
				end
			end
		end
	end
end)

Hope this helps.

If you have any questions just post a comment to this answer and I'll try to answer them.

2 comments

0

Is there anyway you could just add to the script I have to make it where the hat and face go invisible as well?

0

Accessories don't have a Transparency option so there's not really any other way to make them 'invisible' other than moving them to another location and back.

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