Soybeen
|
  |
| Joined: 17 Feb 2010 |
| Total Posts: 21462 |
|
|
| 15 May 2016 06:43 PM |
function onKeyPress(inputObject, gameProcessedEvent) if inputObject.KeyCode == Enum.KeyCode.One then -- get tool 1 elseif inputObject.KeyCode == Enum.KeyCode.Two then -- get tool 2 elseif inputObject.KeyCode == Enum.KeyCode.Three then -- get tool 3 -- Continue like this until tool 9 end end
So, How could I make this more streamlined?
|
|
|
| Report Abuse |
|
|
eRanged
|
  |
| Joined: 15 Jun 2013 |
| Total Posts: 9746 |
|
| |
|
Soybeen
|
  |
| Joined: 17 Feb 2010 |
| Total Posts: 21462 |
|
| |
|
|
| 15 May 2016 06:46 PM |
| Yes, I'll write the script in a bit. |
|
|
| Report Abuse |
|
|
eRanged
|
  |
| Joined: 15 Jun 2013 |
| Total Posts: 9746 |
|
|
| 15 May 2016 06:48 PM |
This is how you would shorter the equip function. Idk how to shorten the keydown though
function Equip(Tool) Humanoid:UnequipTools() local wepLocation = game.ServerStorage:FindFirstChild(Tool) local newWep = wepLocation:Clone() newWep.Parent = Backpack Humanoid:EquipTool(newWep) end
if key == 1 then Equip("ToolOne") elseif key == 2 then Equip("ToolTwo") end |
|
|
| Report Abuse |
|
|
GGGGG14
|
  |
| Joined: 29 Jan 2012 |
| Total Posts: 25344 |
|
|
| 15 May 2016 06:49 PM |
local table = {Enum.KeyCode.One,Enum.KeyCode.Two} -- Of course ur gunna add all 1-9 x_x
function onKeyPress(inputObject, gameProcessedEvent) for i,v in pairs(table) do if inputOject == v then -- bleh end end end
probably not helping at all because it's still not streamlined <3 |
|
|
| Report Abuse |
|
|
|
| 15 May 2016 06:58 PM |
Use this to start.
UIS = game:GetService("UserInputService") p = game.Players.LocalPlayer function GetTool(key) for i,v in pairs(p.Character) do if v:IsA("Tool") then v.Parent = p.Backpack end end p.BackPack[tostring(key)].Parent = p.Character end
UIS.InputBegan:connect(function(Input) key = tonumber(Input.KeyCode.Value) - 48 if tonumber(Input.KeyCode.Value) - 48 > 0 and tonumber(Input.KeyCode.Value) - 48 <= 9 then print(key) GetTool(Key) end end) |
|
|
| Report Abuse |
|
|
|
| 15 May 2016 07:01 PM |
Forgot to mention this belongs in a server script, and here's a better version.
tools = {}
UIS = game:GetService("UserInputService") p = game.Players.LocalPlayer function GetTool(key) for i,v in pairs(p.Character) do if v:IsA("Tool") then v.Parent = p.Backpack end end tools[key].Parent = p.Character end
UIS.InputBegan:connect(function(Input) key = tonumber(Input.KeyCode.Value) - 48 if key > 0 and key <= 9 then print(key) GetTool(key) end end) |
|
|
| Report Abuse |
|
|
|
| 15 May 2016 07:04 PM |
Oops, fixed.
tools = {script.Parent:WaitForChild("Tool")}
UIS = game:GetService("UserInputService") p = game.Players.LocalPlayer function GetTool(key) for i,v in pairs(p.Character:GetChildren()) do if v:IsA("Tool") then v.Parent = p.Backpack end end tools[key].Parent = p.Character end
UIS.InputBegan:connect(function(Input) key = tonumber(Input.KeyCode.Value) - 48 if key > 0 and key <= 9 then print(key) GetTool(key) end end) |
|
|
| Report Abuse |
|
|
Soybeen
|
  |
| Joined: 17 Feb 2010 |
| Total Posts: 21462 |
|
|
| 15 May 2016 07:07 PM |
Thanks for the help guys, I'll implement this and see what works. :)
|
|
|
| Report Abuse |
|
|
Soybeen
|
  |
| Joined: 17 Feb 2010 |
| Total Posts: 21462 |
|
|
| 15 May 2016 07:35 PM |
So, Prox, where exactly would your code live?
I see at top it says tools = {script.Parent:WaitForChild("Tool")} but I'm looking to run this from a large "LocalHandler" script I have in the StarterPlayerScripts.
The system would allow for tools to be added into the player's Backpack at random as they create them, so I can't define them in a table, I'd just have to reference the backpack
|
|
|
| Report Abuse |
|
|
|
| 15 May 2016 07:45 PM |
tools = {}
game.Players.LocalPlayer.Backpack:ChildAddedconnect(function(child) if child:IsA("Tool") then tools[#tools + 1] = child end end) |
|
|
| Report Abuse |
|
|
Soybeen
|
  |
| Joined: 17 Feb 2010 |
| Total Posts: 21462 |
|
|
| 15 May 2016 07:45 PM |
If something was already in the Backpack (because of StarterPack), would your code include it?
|
|
|
| Report Abuse |
|
|
|
| 15 May 2016 07:46 PM |
tools = {}
game.Players.LocalPlayer.Backpack.ChildAdded:connect(function(child) if child:IsA("Tool") then tools[#tools + 1] = child end end) |
|
|
| Report Abuse |
|
|
Soybeen
|
  |
| Joined: 17 Feb 2010 |
| Total Posts: 21462 |
|
|
| 15 May 2016 07:47 PM |
So, is that a yes?
The ChildAdded says to me that a child must be added to the backpack for it to recognize it - what about something that *might* be there before this code executes?
|
|
|
| Report Abuse |
|
|
|
| 15 May 2016 07:49 PM |
It should, yes.
If not
add in
for i,v in pairs(game.Players.LocalPlayer.Backpack:GetChildren()) do if v:IsA("Tool") then tools[#tools + 1] = v end end
Writing this on a new keyboard so sorry for mistakes. |
|
|
| Report Abuse |
|
|
|
| 15 May 2016 07:51 PM |
To summarize.
UIS = game:GetService("UserInputService") p = game.Players.LocalPlayer
tools = {}
for i,v in pairs(game.Players.LocalPlayer.Backpack:GetChildren()) do if v:IsA("Tool") then tools[#tools + 1] = v end end
game.Players.LocalPlayer.Backpack.ChildAdded:connect(function(child) if child:IsA("Tool") then tools[#tools + 1] = child end end)
function GetTool(key) for i,v in pairs(p.Character:GetChildren()) do if v:IsA("Tool") then v.Parent = p.Backpack end end tools[key].Parent = p.Character end
UIS.InputBegan:connect(function(Input) key = tonumber(Input.KeyCode.Value) - 48 if key > 0 and key <= 9 then print(key) GetTool(key) end end) |
|
|
| Report Abuse |
|
|