Basically, a script that makes the player invisible by pressing a key but, pressing it again makes the character visible?
how would i turn this code into that?
01 | game.Players.PlayerAdded:Connect( function (plr) |
02 | plr.CharacterAdded:Connect( function (char) |
03 | for i,Child in pairs (char:GetChildren()) do |
04 | if Child:IsA( 'Part' ) or Child:IsA( 'MeshPart' ) then |
05 | Child.Transparency = 1 |
06 | end |
07 | end |
08 | wait( 1.25 ) |
09 | for i,Child in pairs (char:GetChildren()) do |
10 | if Child:IsA( 'Accessory' ) then |
11 | Child:Destroy() |
12 | end |
13 | end |
14 | end ) |
15 | end ) |
First of all, Make a local script and put it in StarterPack or StarterPlayerScripts
01 | local UIS = game:GetService( "UserInputService" ) -- Get user input service |
02 | local Visible = true |
03 | local player = game.Players.LocalPlayer |
04 | UIS.InputBegan:Connect( function (key) -- when player presses a key |
05 | if key.KeyCode = = Enum.KeyCode.R then -- Change R to the key you want |
06 | if Visible = = true then |
07 | game.ReplicatedStorage.RemoteEvent 1 :FireServer() -- We have to fire server |
08 | Visible = false |
09 | elseif Visible = = false then |
10 | game.ReplicatedStorage.RemoteEvent 2 :FireServer() |
11 | Visible = true |
12 | end |
13 | end |
14 | end ) |
You need to make 2 RemoteEvents. Put them both in ReplicatedStorage. Name the first "RemoteEvent1" And the second "RemoteEvent2"
Now create a server script ("script") and put it in ServerScriptService
1 | game.ReplicatedStorage.RemoteEvent 1. OnServerEvent:Connect( function (plr) |
2 | for i,v in pairs (game.Workspace [ plr.Name ] :GetDescendants()) do |
3 | if v:IsA( "Part" ) or v:IsA( "MeshPart" ) then |
4 | v.Transparency = 1 -- this makes them invisible |
5 | end |
6 | end |
7 | end ) |
Then make another server script and put it in the same place. Make the code for that script
1 | game.ReplicatedStorage.RemoteEvent 2. OnServerEvent:Connect( function (plr) |
2 | for i,v in pairs (game.Workspace [ plr.Name ] :GetDescendants()) do |
3 | if v:IsA( "Part" ) or v:IsA( "MeshPart" ) then |
4 | v.Transparency = 0 |
5 | end |
6 | end |
7 | end ) |
I've tested this out and it worked, enjoy
you have to use userinputService. You also need a remote event if you want everyone to see your character invisible,
localScript in startercharacterScripts
01 | local player = game.Players.LocalPlayer |
02 | local character = script.Parent -- Since the script will be inside the character, easier way is to use script.Parent |
03 | repeat wait() until character:FindFirstChild( "Humanoid" ) |
04 | local UserInputService = game:GetService( "UserInputService" ) |
05 | local isInvisible = false |
06 |
07 | UserInputService.InputBegan:Connect( function (input,gameProcessedEvent) -- input is the button the player pressed and gameProcessedEvent makes sure that you are not chatting, since if you dont use it players will accidentally press a button while chatting |
08 | if input.KeyCode = = Enum.KeyCode.L then -- Your button for making the character invisible |
09 | game:GetService( "ReplicatedStorage" ).RemoteEvent:FireServer(isInvisible) |
10 | end |
11 | end ) |
Server Script:
01 | local ReplicatedStorage = game:GetService( "ReplicatedStorage" ) |
02 | local RemoteEvent = ReplicatedStorage:WaitForChild( "RemoteEvent" ) |
03 |
04 | RemoteEvent.OnServerEvent:Connect( function (player,isInvisible) -- In Server Event, the first parameter is always the player |
05 | if not isInvisible then |
06 | isInvisible = true |
07 | for _,v in pairs (player.Character:GetDescendants()) do -- Descendants are ALL the parts including the children of the children |
08 | if v:IsA( "BasePart" ) or v:IsA( "Decal" ) then -- Because parts and decals both have transparency, this will make the script shorter |
09 | v.Transparency = 1 |
10 | elseif v:IsA( "Accessory" ) then |
11 | v:FindFirstChild( "Handle" ).Transparency = 1 |
12 | end |
13 | end |
14 | elseif isInvisible then |
15 | isInvisible = false |