Help with script

Make players invisible through a button

– What I want to achieve:

To be able to fully make a player invisible and THEN visible if the button is pressed, again.


The issue:

I cannot work this script no matter what I try:

Server script:

game.ReplicatedStorage.Invisible.OnServerEvent:Connect(function(Player,status)
	local char = Player.Character
	if status == true then
		reappear(char)
	elseif status == false then
		dissapear(char)
	end
end)
function dissapear(char)
	for _,part in next,char:GetDescendants() do
		if part:IsA("BasePart") then
			part.Transparency = 1
			char.Head.face.Transparency = 1
		end
	end
end
function reappear(char)
	for _,part in next,char:GetDescendants() do
		if part:IsA("BasePart") then
			part.Transparency = 0
			char.HumanoidRootPart.Transparency = 1
			char.Head.face.Transparency = 0
		end
	end
end

Inside the button, local script:

local player = game.Players.LocalPlayer
local invis = false
local char = player.Character

local user = script.Parent.Parent.PlayerUsername

script.Parent.MouseButton1Click:Connect(function()
	if invis == false then
		dissapear()
	elseif invis == true then
		reappear()
	end
end)
function dissapear()
	if invis == false then
		game.ReplicatedStorage.Invisible:FireServer(user.Text, invis)
		invis = true
		script.Parent.Text = "Visible"
	end
end
function reappear()
	if invis == true then
		game.ReplicatedStorage.Invisible:FireServer(user.Text, invis)
		invis = false
		script.Parent.Text = "Invisible"
	end
end

Did it say any errors? If so can u put it down

Why are you firing the server with “user.Text”, you just want to have “invis” as the only parameter. The problem is that the server checks if “user.Text” == false or true", ir is neither of those so it never runs after those if statements.

1 Like

No errors, unfortunately. I tried looking many times;

I wanted to put it through my admin panel where it has to type the username first, beforehand.

Errors as in when you run the game

You are calling functions before they are even defined

There is no errors, as I said.

How would I resolve that then?

Define the functions and run the code after, in other words, rearrange your code

That was only part of the code, I have other code that I used beforehand I’ll send it over if needed.

Btw Can I see your descendants because I am confused

Other code shouldn’t be the problem; you need to keep in mind that the top half of the code will run first, so you cannot reference something that hasn’t been defined as it hasn’t even run yet.

2 Likes

So when you fire a remoteevent, the first parameter is always the player who sent it. Try this:

local function checkForPlayers(name)
     for I, v in pairs(game.Players:GetChildren()) do
          if v.Name:lower() == name:lower() then
               return v
          end
     end
end

game.ReplicatedStorage.Invisible.OnServerEvent:Connect(function(Playerwhosent,playername,status)
	local player = checkForPlayers(playername)
        if player then
       local char = player.Character
	if status == true then
		reappear(char)
	elseif status == false then
		dissapear(char)
	end
        end
end)

function dissapear(char)
	for _,part in next,char:GetDescendants() do
		if part:IsA("BasePart") then
			part.Transparency = 1
			char.Head.face.Transparency = 1
		end
	end
end
function reappear(char)
	for _,part in next,char:GetDescendants() do
		if part:IsA("BasePart") then
			part.Transparency = 0
			char.HumanoidRootPart.Transparency = 1
			char.Head.face.Transparency = 0
		end
	end
end
1 Like

Where exactly, would I put that?

Just replace your old server script with this.

1 Like

Alright, what about the local script?

It should be fine the way it is.

1 Like

@walshuk The functions are global functions, because he doesn’t user “local” inside of them. Therefore, it ok to call them above where they are defined.

2 Likes

Alright, testing now Thank you. Amazing it worked!

1 Like