Advertisement
StarzoZero

Roblox Mokrios' FE Code (Better Version)

Jan 17th, 2019
386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.78 KB | None | 0 0
  1. -- READ THIS PART
  2. -- TO GET THIS TO WORK JUST PASTE IT AT THE TOP OF YOUR SCRIPT AND SAVE IT
  3. -- NOW DELETE THESE LINES IF YOU WANT
  4.  
  5.  
  6. if game:GetService("RunService"):IsClient() then error("Script must be server-side in order to work; use h/ and not hl/") end
  7. local Player,game,owner = owner,game
  8. local RealPlayer = Player
  9. do
  10. print("FE Compatibility code by Mokiros, Edited by StarzoZero")
  11. local rp = RealPlayer
  12. script.Parent = rp.Character
  13.  
  14. --RemoteEvent for communicating
  15. local Event = Instance.new("RemoteEvent")
  16. Event.Name = "UserInput_Event"
  17.  
  18. --Fake event to make stuff like Mouse.KeyDown work
  19. local function fakeEvent()
  20. local t = {_fakeEvent=true,Functions={},Connect=function(self,f)table.insert(self.Functions,f) end}
  21. t.connect = t.Connect
  22. return t
  23. end
  24.  
  25. --Creating fake input objects with fake variables
  26. local m = {Target=nil,Hit=CFrame.new(),KeyUp=fakeEvent(),KeyDown=fakeEvent(),Button1Up=fakeEvent(),Button1Down=fakeEvent(),Button2Up=fakeEvent(),Button2Down=fakeEvent()}
  27. local UIS = {InputBegan=fakeEvent(),InputEnded=fakeEvent()}
  28. local CAS = {Actions={},BindAction=function(self,name,fun,touch,...)
  29. CAS.Actions[name] = fun and {Name=name,Function=fun,Keys={...}} or nil
  30. end}
  31. --Merged 2 functions into one by checking amount of arguments
  32. CAS.UnbindAction = CAS.BindAction
  33.  
  34. --This function will trigger the events that have been :Connect()'ed
  35. local function te(self,ev,...)
  36. local t = m[ev]
  37. if t and t._fakeEvent then
  38. for _,f in pairs(t.Functions) do
  39. f(...)
  40. end
  41. end
  42. end
  43. m.TrigEvent = te
  44. UIS.TrigEvent = te
  45.  
  46. Event.OnServerEvent:Connect(function(plr,io)
  47. if plr~=rp then return end
  48. m.Target = io.Target
  49. m.Hit = io.Hit
  50. if not io.isMouse then
  51. local b = io.UserInputState == Enum.UserInputState.Begin
  52. if io.UserInputType == Enum.UserInputType.MouseButton1 then
  53. return m:TrigEvent(b and "Button1Down" or "Button1Up")
  54. end
  55. if io.UserInputType == Enum.UserInputType.MouseButton2 then
  56. return m:TrigEvent(b and "Button2Down" or "Button2Up")
  57. end
  58. for _,t in pairs(CAS.Actions) do
  59. for _,k in pairs(t.Keys) do
  60. if k==io.KeyCode then
  61. t.Function(t.Name,io.UserInputState,io)
  62. end
  63. end
  64. end
  65. m:TrigEvent(b and "KeyDown" or "KeyUp",io.KeyCode.Name:lower())
  66. UIS:TrigEvent(b and "InputBegan" or "InputEnded",io,false)
  67. end
  68. end)
  69. Event.Parent = NLS([==[
  70. local Player = game:GetService("Players").LocalPlayer
  71. local Event = script:WaitForChild("UserInput_Event")
  72. local Mouse = Player:GetMouse()
  73. local UIS = game:GetService("UserInputService")
  74. local input = function(io,a)
  75. if a then return end
  76. --Since InputObject is a client-side instance, we create and pass table instead
  77. Event:FireServer({KeyCode=io.KeyCode,UserInputType=io.UserInputType,UserInputState=io.UserInputState,Hit=Mouse.Hit,Target=Mouse.Target})
  78. end
  79. UIS.InputBegan:Connect(input)
  80. UIS.InputEnded:Connect(input)
  81. local h,t
  82. --Give the server mouse data 30 times every second, but only if the values changed
  83. --If player is not moving their mouse, client won't fire events
  84. while wait(1/30) do
  85. if h~=Mouse.Hit or t~=Mouse.Target then
  86. h,t=Mouse.Hit,Mouse.Target
  87. Event:FireServer({isMouse=true,Target=t,Hit=h})
  88. end
  89. end]==],Player.Character)
  90.  
  91. ----Sandboxed game object that allows the usage of client-side methods and services
  92. --Real game object
  93. local _rg = game
  94.  
  95. --Metatable for fake service
  96. local fsmt = {
  97. __index = function(self,k)
  98. local s = rawget(self,"_RealService")
  99. if s then
  100. return typeof(s[k])=="function"
  101. and function(_,...)return s[k](s,...)end or s[k]
  102. end
  103. end,
  104. __newindex = function(self,k,v)
  105. local s = rawget(self,"_RealService")
  106. if s then s[k]=v end
  107. end
  108. }
  109. local function FakeService(t,RealService)
  110. t._RealService = typeof(RealService)=="string" and _rg:GetService(RealService) or RealService
  111. return setmetatable(t,fsmt)
  112. end
  113.  
  114. --Fake game object
  115. local g = {
  116. GetService = function(self,s)
  117. return rawget(self,s) or _rg:GetService(s)
  118. end,
  119. Players = FakeService({
  120. LocalPlayer = FakeService({GetMouse=function(self)return m end},Player)
  121. },"Players"),
  122. UserInputService = FakeService(UIS,"UserInputService"),
  123. ContextActionService = FakeService(CAS,"ContextActionService"),
  124. RunService = FakeService({
  125. _btrs = {},
  126. RenderStepped = _rg:GetService("RunService").Heartbeat,
  127. BindToRenderStep = function(self,name,_,fun)
  128. self._btrs[name] = self.Heartbeat:Connect(fun)
  129. end,
  130. UnbindFromRenderStep = function(self,name)
  131. self._btrs[name]:Disconnect()
  132. end,
  133. },"RunService")
  134. }
  135. rawset(g.Players,"localPlayer",g.Players.LocalPlayer)
  136. g.service = g.GetService
  137. FakeService(g,game)
  138. --Changing owner to fake player object to support owner:GetMouse()
  139. game,owner = g,g.Players.LocalPlayer
  140. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement