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:
function onKeyPress(inputObject, gameProcessedEvent) if inputObject.KeyCode == Enum.KeyCode.Return then if theText== "Handle" then local cloneObj= game.ReplicatedStorage:FindFirstChild("Handle",true):Clone() cloneObj.Parent= game.StarterGui.Inventory.Itembag elseif theText== "Med Handle" then local cloneObj= game.ReplicatedStorage:FindFirstChild("Med Handle",true):Clone() cloneObj.Parent= game.StarterGui.Inventory.Itembag elseif theText== "Large Handle" then local cloneObj= game.ReplicatedStorage:FindFirstChild("Large Handle",true):Clone() cloneObj.Parent= game.StarterGui.Inventory.Itembag elseif theText== "Hull" then local theClonedItem= theText createItemCloneEvent:FireServer(theClonedItem) --etc etc etc end end end
My attempt was to rewrite all the items in the beginning, like this:
function onKeyPress(inputObject, gameProcessedEvent) if inputObject.KeyCode == Enum.KeyCode.Return then if theText== "Handle" or theText== "Med Handle" or theText== "Large Handle" or theText== "Hull" then --local theClonedItem= theText createItemCloneEvent:FireServer(theClonedItem) end end 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:
local items = {"Handle","Med Handle","Large Handle","Hull"} --Add all the items in this function onKeyPress(inputObject, gameProcessedEvent) if inputObject.KeyCode == Enum.KeyCode.Return then for i,v in pairs(items) do if theText == v then local cloneObj = game.ReplicatedStorage:FindFirstChild(v,true):Clone() cloneObj.Parent= game.StarterGui.Inventory.Itembag createItemCloneEvent:FireServer(theText) break end end end 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:
local locationForItems = game.ServerStorage.ItemFolder -- this is an example -- this loop should fire on your event or whatever. It can be in a function if you like. for i,v in pairs(locationForItems:GetChildren()) do -- this is all very simplistic you can edit it to suit your needs if typedText == v then -- clone the item end end
Hope this helps. Have a great day scripting!