DrawingJhon

Local server side script

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