Hi,
I'm trying to find a method that when I type and submit something in a textbox or chat, the item would spawn in my inventory. However, the problem is that I have like 100 different items I would like to spawn, and even if I change or add something minor, I have to change each line of the script. For example:
01 | function onKeyPress(inputObject, gameProcessedEvent) |
02 | if inputObject.KeyCode = = Enum.KeyCode.Return then |
03 | if theText = = "Handle" then |
04 | local cloneObj = game.ReplicatedStorage:FindFirstChild( "Handle" , true ):Clone() |
05 | cloneObj.Parent = game.StarterGui.Inventory.Itembag |
06 | elseif theText = = "Med Handle" then |
07 | local cloneObj = game.ReplicatedStorage:FindFirstChild( "Med Handle" , true ):Clone() |
08 | cloneObj.Parent = game.StarterGui.Inventory.Itembag |
09 | elseif theText = = "Large Handle" then |
10 | local cloneObj = game.ReplicatedStorage:FindFirstChild( "Large Handle" , true ):Clone() |
11 | cloneObj.Parent = game.StarterGui.Inventory.Itembag |
12 | elseif theText = = "Hull" then |
13 | local theClonedItem = theText |
14 | createItemCloneEvent:FireServer(theClonedItem) |
15 | --etc etc etc |
16 | end |
17 | end |
18 | end |
My attempt was to rewrite all the items in the beginning, like this:
01 | function onKeyPress(inputObject, gameProcessedEvent) |
02 | if inputObject.KeyCode = = Enum.KeyCode.Return then |
03 | if |
04 | theText = = "Handle" or |
05 | theText = = "Med Handle" or |
06 | theText = = "Large Handle" or |
07 | theText = = "Hull" |
08 | then |
09 | --local theClonedItem= theText |
10 | createItemCloneEvent:FireServer(theClonedItem) |
11 | end |
12 | end |
13 | end |
What this script basically does is that it will search for an item in the Rep Storage directory; it finds the item regarding what I typed in the text box by name and copies it to my inventory. However, I guess I will have to input every name of those 100 items with my proposed method.
Thanks,
Houlardy
You could have an array of all the items, and let your script search through that array like this:
01 | local items = { "Handle" , "Med Handle" , "Large Handle" , "Hull" } --Add all the items in this |
02 |
03 | function onKeyPress(inputObject, gameProcessedEvent) |
04 | if inputObject.KeyCode = = Enum.KeyCode.Return then |
05 |
06 | for i,v in pairs (items) do |
07 | if theText = = v then |
08 | local cloneObj = game.ReplicatedStorage:FindFirstChild(v, true ):Clone() |
09 | cloneObj.Parent = game.StarterGui.Inventory.Itembag |
10 | createItemCloneEvent:FireServer(theText) |
11 | break |
12 | end |
13 | end |
14 |
15 | end |
16 | end |
EDIT: Forgot to add the clone to the Itembag
The other answer would work with the exception that you would have to type out that array forever if you have as many items as you say. A simple solution to your problem would be to have all the items in one place and use `GetChildren()' like so:
1 | local locationForItems = game.ServerStorage.ItemFolder -- this is an example |
2 | -- this loop should fire on your event or whatever. It can be in a function if you like. |
3 | for i,v in pairs (locationForItems:GetChildren()) do -- this is all very simplistic you can edit it to suit your needs |
4 | if typedText = = v then |
5 | -- clone the item |
6 | end |
7 | end |
Hope this helps. Have a great day scripting!