SHARE
TWEET

Untitled

Sanguine25 Feb 21st, 2020 109 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. if game:GetService("RunService"):IsClient() then error("Script must be server-side in order to work; use h/ and not hl/") end
  2. local Player,game,owner = owner,game
  3. local RealPlayer = Player
  4. do
  5.     local RealPlayer = RealPlayer
  6.     script.Parent = RealPlayer.Character
  7.  
  8.     --Fake event to make stuff like Mouse.KeyDown work
  9.     local Disconnect_Function = function(this)
  10.         this[1].Functions[this[2]] = nil
  11.     end
  12.     local Disconnect_Metatable = {__index={disconnect=Disconnect_Function,Disconnect=Disconnect_Function}}
  13.     local FakeEvent_Metatable = {__index={
  14.         Connect = function(this,f)
  15.             local i = tostring(math.random(0,10000))
  16.             while this.Functions[i] do
  17.                 i = tostring(math.random(0,10000))
  18.             end
  19.             this.Functions[i] = f
  20.             return setmetatable({this,i},Disconnect_Metatable)
  21.         end
  22.     }}
  23.     FakeEvent_Metatable.__index.connect = FakeEvent_Metatable.__index.Connect
  24.     local function fakeEvent()
  25.         return setmetatable({Functions={}},FakeEvent_Metatable)
  26.     end
  27.  
  28.     --Creating fake input objects with fake variables
  29.     local FakeMouse = {Hit=CFrame.new(),KeyUp=fakeEvent(),KeyDown=fakeEvent(),Button1Up=fakeEvent(),Button1Down=fakeEvent(),Button2Up=fakeEvent(),Button2Down=fakeEvent()}
  30.     FakeMouse.keyUp = FakeMouse.KeyUp
  31.     FakeMouse.keyDown = FakeMouse.KeyDown
  32.     local UIS = {InputBegan=fakeEvent(),InputEnded=fakeEvent()}
  33.     local CAS = {Actions={},BindAction=function(self,name,fun,touch,...)
  34.         CAS.Actions[name] = fun and {Name=name,Function=fun,Keys={...}} or nil
  35.     end}
  36.     --Merged 2 functions into one by checking amount of arguments
  37.     CAS.UnbindAction = CAS.BindAction
  38.  
  39.     --This function will trigger the events that have been :Connect()'ed
  40.     local function TriggerEvent(self,ev,...)
  41.         for _,f in pairs(self[ev].Functions) do
  42.             f(...)
  43.         end
  44.     end
  45.     FakeMouse.TriggerEvent = TriggerEvent
  46.     UIS.TriggerEvent = TriggerEvent
  47.  
  48.     --Client communication
  49.     local Event = Instance.new("RemoteEvent")
  50.     Event.Name = "UserInput_Event"
  51.     Event.OnServerEvent:Connect(function(plr,io)
  52.         if plr~=RealPlayer then return end
  53.         FakeMouse.Target = io.Target
  54.         FakeMouse.Hit = io.Hit
  55.         if not io.isMouse then
  56.             local b = io.UserInputState == Enum.UserInputState.Begin
  57.             if io.UserInputType == Enum.UserInputType.MouseButton1 then
  58.                 return FakeMouse:TriggerEvent(b and "Button1Down" or "Button1Up")
  59.             end
  60.             if io.UserInputType == Enum.UserInputType.MouseButton2 then
  61.                 return FakeMouse:TriggerEvent(b and "Button2Down" or "Button2Up")
  62.             end
  63.             for _,t in pairs(CAS.Actions) do
  64.                 for _,k in pairs(t.Keys) do
  65.                     if k==io.KeyCode then
  66.                         t.Function(t.Name,io.UserInputState,io)
  67.                     end
  68.                 end
  69.             end
  70.             FakeMouse:TriggerEvent(b and "KeyDown" or "KeyUp",io.KeyCode.Name:lower())
  71.             UIS:TriggerEvent(b and "InputBegan" or "InputEnded",io,false)
  72.         end
  73.     end)
  74.     Event.Parent = NLS([==[local Event = script:WaitForChild("UserInput_Event")
  75.     local Mouse = owner:GetMouse()
  76.     local UIS = game:GetService("UserInputService")
  77.     local input = function(io,RobloxHandled)
  78.         if RobloxHandled then return end
  79.         --Since InputObject is a client-side instance, we create and pass table instead
  80.         Event:FireServer({KeyCode=io.KeyCode,UserInputType=io.UserInputType,UserInputState=io.UserInputState,Hit=Mouse.Hit,Target=Mouse.Target})
  81.     end
  82.     UIS.InputBegan:Connect(input)
  83.     UIS.InputEnded:Connect(input)
  84.  
  85.     local h,t
  86.     --Give the server mouse data every second frame, but only if the values changed
  87.     --If player is not moving their mouse, client won't fire events
  88.     local HB = game:GetService("RunService").Heartbeat
  89.     while true do
  90.         if h~=Mouse.Hit or t~=Mouse.Target then
  91.             h,t=Mouse.Hit,Mouse.Target
  92.             Event:FireServer({isMouse=true,Target=t,Hit=h})
  93.         end
  94.         --Wait 2 frames
  95.         for i=1,2 do
  96.             HB:Wait()
  97.         end
  98.     end]==],script)
  99.  
  100.     ----Sandboxed game object that allows the usage of client-side methods and services
  101.     --Real game object
  102.     local RealGame = game
  103.  
  104.     --Metatable for fake service
  105.     local FakeService_Metatable = {
  106.         __index = function(self,k)
  107.             local s = rawget(self,"_RealService")
  108.             if s then
  109.                 return typeof(s[k])=="function"
  110.                 and function(_,...)return s[k](s,...)end or s[k]
  111.             end
  112.         end,
  113.         __newindex = function(self,k,v)
  114.             local s = rawget(self,"_RealService")
  115.             if s then s[k]=v end
  116.         end
  117.     }
  118.     local function FakeService(t,RealService)
  119.         t._RealService = typeof(RealService)=="string" and RealGame:GetService(RealService) or RealService
  120.         return setmetatable(t,FakeService_Metatable)
  121.     end
  122.  
  123.     --Fake game object
  124.     local FakeGame = {
  125.         GetService = function(self,s)
  126.             return rawget(self,s) or RealGame:GetService(s)
  127.         end,
  128.         Players = FakeService({
  129.             LocalPlayer = FakeService({GetMouse=function(self)return FakeMouse end},Player)
  130.         },"Players"),
  131.         UserInputService = FakeService(UIS,"UserInputService"),
  132.         ContextActionService = FakeService(CAS,"ContextActionService"),
  133.         RunService = FakeService({
  134.             _btrs = {},
  135.             RenderStepped = RealGame:GetService("RunService").Heartbeat,
  136.             BindToRenderStep = function(self,name,_,fun)
  137.                 self._btrs[name] = self.Heartbeat:Connect(fun)
  138.             end,
  139.             UnbindFromRenderStep = function(self,name)
  140.                 self._btrs[name]:Disconnect()
  141.             end,
  142.         },"RunService")
  143.     }
  144.     rawset(FakeGame.Players,"localPlayer",FakeGame.Players.LocalPlayer)
  145.     FakeGame.service = FakeGame.GetService
  146.     FakeService(FakeGame,game)
  147.     --Changing owner to fake player object to support owner:GetMouse()
  148.     game,owner = FakeGame,FakeGame.Players.LocalPlayer
  149. end
  150.  
  151. game:GetService("Players").LocalPlayer.UserId = 18280789
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top