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
 

Re: Custom Toolbar - alternative to this gross code?

Previous Thread :: Next Thread 
Soybeen is not online. 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 is not online. eRanged
Joined: 15 Jun 2013
Total Posts: 9746
15 May 2016 06:43 PM
function
Report Abuse
Soybeen is not online. Soybeen
Joined: 17 Feb 2010
Total Posts: 21462
15 May 2016 06:44 PM
Pardon?


Report Abuse
Proxidious is not online. Proxidious
Joined: 18 Jul 2011
Total Posts: 1339
15 May 2016 06:46 PM
Yes, I'll write the script in a bit.
Report Abuse
eRanged is not online. 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 is not online. 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
Proxidious is not online. Proxidious
Joined: 18 Jul 2011
Total Posts: 1339
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
Proxidious is not online. Proxidious
Joined: 18 Jul 2011
Total Posts: 1339
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
Proxidious is not online. Proxidious
Joined: 18 Jul 2011
Total Posts: 1339
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 is not online. 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 is not online. 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
Proxidious is not online. Proxidious
Joined: 18 Jul 2011
Total Posts: 1339
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 is not online. 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
Proxidious is not online. Proxidious
Joined: 18 Jul 2011
Total Posts: 1339
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 is not online. 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
Proxidious is not online. Proxidious
Joined: 18 Jul 2011
Total Posts: 1339
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
Proxidious is not online. Proxidious
Joined: 18 Jul 2011
Total Posts: 1339
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
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