generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Can I make this more efficient?

Previous Thread :: Next Thread 
epicbreaker is not online. epicbreaker
Joined: 23 Apr 2011
Total Posts: 2791
01 Jul 2015 03:18 PM
So, I'm making a module which should turn UserInputService events in to a more "beginner" friendly version (PlayerMouse).

Is there any way I can improve this?
----------------------------------
return function()
local Mouse = game.Players.LocalPlayer:GetMouse()
local MousePos = Vector2.new(Mouse.X, Mouse.Y)

local Connections = {}
local Events = {
LeftClick = {connect = function(self, func) Connections.LeftClick = func end},
RightClick = {connect = function(self, func) Connections.RightClick = func end},
LeftDown = {connect = function(self, func) Connections.LeftDown = func end},
RightDown = {connect = function(self, func) Connections.RightDown = func end},
LeftUp = {connect = function(self, func) Connections.LeftUp = func end},
RightUp = {connect = function(self, func) Connections.RightUp = func end},
Moved = {connect = function(self, func) Connections.Moved = func end},
LeftDrag = {connect = function(self, func) Connections.LeftDrag = func end},
RightDrag = {connect = function(self, func) Connections.RightDrag = func end},
KeyPressed = {connect = function(self, func) Connections.KeyPressed = func end},
KeyReleased = {connect = function(self, func) Connections.KeyReleased = func end},
WheelScroll = {connect = function(self, func) Connections.WheelScroll = func end},
}

for i,a in pairs(Events) do
Events[i]:connect(function() end)
end

local MouseClicks = {MouseButton1=false, MouseButton2=false}
local Click = {0, 0}

game["Run Service"].RenderStepped:connect(function()
if MouseClicks.MouseButton1 then
Click[1]= Click[1] + game.Workspace:GetRealPhysicsFPS()/60
else
Click[1] = 100
end
if MouseClicks.MouseButton2 then
Click[2] = Click[2] + game.Workspace:GetRealPhysicsFPS()/60
else
Click[2] = 100
end
end)

game:GetService("UserInputService").InputChanged:connect(function(Input, GPE)
if not GPE then
if Input.UserInputType == Enum.UserInputType.MouseMovement then
local FakeMouse = {
Delta = Vector2.new(Mouse.X - MousePos.X, Mouse.Y - MousePos.Y)
}
Connections.Moved(FakeMouse)
MousePos = Vector2.new(Mouse.X, Mouse.Y)
if MouseClicks.MouseButton1 then
Connections.LeftDrag(FakeMouse)
end
if MouseClicks.MouseButton2 then
Connections.RightDrag(FakeMouse)
end
elseif Input.UserInputType == Enum.UserInputType.MouseWheel then
local FakeMouse = {
Delta = Vector2.new(0, Input.Position.Z)
}
Connections.WheelScroll(FakeMouse)
end
end
end)

game:GetService("UserInputService").InputBegan:connect(function(Input, GPE)
if not GPE then
if Input.UserInputType == Enum.UserInputType.MouseButton1 or Input.UserInputType == Enum.UserInputType.MouseButton2 then
Click[tonumber(string.match(Input.UserInputType.Name, "%d"))] = 0
local tab = {"Left", "Right"}
Connections[tab[tonumber(string.match(Input.UserInputType.Name, "%d"))].."Down"]()
MouseClicks[Input.UserInputType.Name] = true
elseif Input.UserInputType == Enum.UserInputType.Keyboard then
Connections.KeyPressed(Input.KeyCode.Name)
end
end
end)

game:GetService("UserInputService").InputEnded:connect(function(Input, GPE)
if not GPE then
if Input.UserInputType == Enum.UserInputType.MouseButton1 or Input.UserInputType == Enum.UserInputType.MouseButton2 then
if Click[1] < 10 then
Connections.LeftClick()
end
if Click[2] < 10 then
Connections.RightClick()
end
local tab = {"Left", "Right"}
Connections[tab[tonumber(string.match(Input.UserInputType.Name, "%d"))].."Up"]()
MouseClicks[Input.UserInputType.Name] = false
elseif Input.UserInputType == Enum.UserInputType.Keyboard then
Connections.KeyReleased(Input.KeyCode.Name)
end
end
end)

return Events
end
Report Abuse
powerhotmail123 is not online. powerhotmail123
Joined: 11 Apr 2011
Total Posts: 5041
01 Jul 2015 03:22 PM
Change:

for i,a in pairs(Events) do
Events[i]:connect(function() end)
end

TO:

for i, a in next, Events do
Events[i]:connect(function() end)
end

More efficient. :P
Report Abuse
epicbreaker is not online. epicbreaker
Joined: 23 Apr 2011
Total Posts: 2791
01 Jul 2015 03:46 PM
Thanks.. qq
Report Abuse
epicbreaker is not online. epicbreaker
Joined: 23 Apr 2011
Total Posts: 2791
01 Jul 2015 04:23 PM
Bump
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image