Advertisement
FairyPrincess11

Invisibility Script

Apr 17th, 2020
27,728
-1
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Shared ModuleScript (in ReplicatedStorage under Folder "shared")
  2.  
  3. return function(Player, Transparency)
  4.    
  5.     -- Get Character and Appropriate Transparency value if not already passed TransparencyVal = (1+1)%2 == 0 or (0+1)%2 == 1
  6.     local Character = Player.Character or Player.CharacterAdded:Wait()
  7.     local TransparencyVal = Transparency or (Character:WaitForChild("Head").Transparency+1)%2
  8.    
  9.     for _, Part in pairs(Character:GetChildren()) do
  10.         -- Srt Transparency of parts according to TransparencyVal
  11.         if (Part:IsA("Part") or Part:IsA("MeshPart")) and Part.Name ~= "HumanoidRootPart" then
  12.             Part.Transparency = TransparencyVal
  13.             if Part.Name == "Head" then
  14.                 -- Edge case; if not modified you would be able to see the face
  15.                 Part:FindFirstChild("face").Transparency = TransparencyVal
  16.             end
  17.         elseif Part:IsA("Accessory") then
  18.             -- Accessories have part handles that can be manipulated like a part
  19.             Part.Handle.Transparency = TransparencyVal
  20.         end
  21.     end
  22. end
  23.  
  24. ------------------------------------------------------------------------------------------------------------------------------
  25.  
  26. -- LocalScript (In StarterPlayerScripts)
  27.  
  28. local ReplicatedStorage, ContextActionService, Players = game:GetService("ReplicatedStorage"), game:GetService("ContextActionService"), game:GetService("Players")
  29.  
  30. local Remotes, SharedModules = ReplicatedStorage:WaitForChild("Remotes"), ReplicatedStorage:WaitForChild("shared")
  31. local Player = Players.LocalPlayer
  32.  
  33. local Transparency = -0.7
  34.  
  35. local function SendReqForInvisibility(ActionName, Input)
  36.     -- The Bind function may be called to make the player visible or invisible
  37.     if ActionName == "Invisibility" and Input == Enum.UserInputState.Begin then
  38.         -- We invoke server to make sure the server makes the player invisible to everybody else
  39.         -- If we did not do this, the server would take longer than the client to make itself invisible, and the client would be invisible even to itself
  40.         Remotes.MakeInvisible:InvokeServer()
  41.         -- Asjust transparency
  42.         Transparency = -Transparency
  43.         -- Same action as server, but we pass a transparency value as an argument
  44.         require(SharedModules.Invisibility)(Player, Transparency)
  45.     end
  46. end
  47.  
  48. -- Binds Keycode E to function "SendReqForInvisibility"
  49. ContextActionService:BindAction("Invisibility", SendReqForInvisibility, false, Enum.KeyCode.E)
  50.  
  51. ------------------------------------------------------------------------------------------------------------------------------
  52.  
  53. -- ServerScript (in ServerScriptService)
  54.  
  55. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  56. local Remotes, SharedModules = ReplicatedStorage:WaitForChild("Remotes"), ReplicatedStorage:WaitForChild("shared")
  57.  
  58. local function MakeInvisible(Player)
  59.     -- Calls the function inside Shared ModuleScript, no transparency value is passed so the module will determine the appropriate value on its own
  60.     return require(SharedModules.Invisibility)(Player)
  61. end
  62.  
  63. Remotes.MakeInvisible.OnServerInvoke = MakeInvisible
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement