Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

What's an efficient method of shortening admin commands with this script?

Asked by 6 years ago

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:

01function 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      
16end
17end
18end

My attempt was to rewrite all the items in the beginning, like this:

01function onKeyPress(inputObject, gameProcessedEvent)
02    if inputObject.KeyCode == Enum.KeyCode.Return then
03        if
04theText== "Handle" or
05theText== "Med Handle" or
06theText== "Large Handle" or
07theText== "Hull"
08then
09    --local theClonedItem= theText
10createItemCloneEvent:FireServer(theClonedItem)
11end
12end
13end

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

0
I answered your question. User#21908 42 — 6y
0
Tried to anyway User#21908 42 — 6y
0
if I helped you out please accept my answer. Otherwise comment how I can help you better User#21908 42 — 6y

2 answers

Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

You could have an array of all the items, and let your script search through that array like this:

01local items = {"Handle","Med Handle","Large Handle","Hull"} --Add all the items in this
02 
03function 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
16end

EDIT: Forgot to add the clone to the Itembag

0
hmm Houlardy642 28 — 6y
0
I was also going to suggest this. Seems to be the way to go. ThatPreston 354 — 6y
Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

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:

1local 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.
3for 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
7end

Hope this helps. Have a great day scripting!

0
So what's the difference if I put it in Server Storage vs Replicated Storage? Houlardy642 28 — 6y
0
I just used serverstorage as an example. replicated storage can be seen by both the client and the server whereas serverstorage cannot be seen by the client. You should probably use replicated storage if you are involving the client in any way User#21908 42 — 6y

Answer this question