Invisibility script works locally but not globally

Hello,
So I am making a button that turns the player invisible (And another that turns them visible again.)
Both of the scripts seemed to be working great until I realised they were only working locally, so the player is still visible for everyone else.

I’m not too savvy with global scrirpts like this, could someone please help me out?

Thank you!

local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()


script.Parent.MouseButton1Click:Connect(function()
	

for _, part in ipairs(Character:GetDescendants()) do
	if part:IsA('BasePart') or part:IsA('Decal') then
		part.Transparency = 1
	end
	end
	
end)
1 Like

In a server script, the first parameter of a ClickDetector event will return as a Player object.

script.Parent.MouseButton1Click:Connect(function(player) --"player" is the player who clicked the button
    for _, part in ipairs(player.Character:GetDescendants()) do
        if part:IsA("Part") or part:IsA("Decal") then
            part.Transparency = 1
        end
    end
end)
1 Like

For future reference, GuiButton.MouseButton1Click is not the same as ClickDetector.MouseClick

Didn’t have studio open, so I was just going by the code above.

Also realized it was a GUI button and not a ClickDetector, because it wasn’t specified

Yes, sorry, I didn’t specify that.

Output seems to be giving an issue for line 2 -
Players.RangerLazarus.PlayerGui.TeamMenu.Abilities.TURN.sc:4: attempt to index nil with ‘Character’

The code works fine for me. Are you trying to do the function before the player’s character is fully loaded? Also, try making the code be run on the server and not a local script.

If the character not being loaded yet is the problem, insert this line of code after script.Parent.MouseButton1Click:Connect(function():

if not Character then warn("Character not loaded!") return end
1 Like

If this is being done within the LocalScript of a GUI, and you wish for all players to be able to see the invisibility, it’s recommended to use RemoteEvents; client-sided scripts (i.e., the LocalScript) will only effect things under the client it belongs to, so won’t be visible to everyone unless the LocalScript calls on a server script
Essentially, for it to work properly, the actual invisibility code will be done inside a server script, and the local script will call upon a RemoteEvent tied to said server script to activate the invisibility
Here are some articles more helpful than myself:

1 Like