Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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

3 comments

0

I answered your question.

0

Tried to anyway

0

if I helped you out please accept my answer. Otherwise comment how I can help you better

2 answers

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

2 comments

0

hmm

0

I was also going to suggest this. Seems to be the way to go.

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!

2 comments

0

So what's the difference if I put it in Server Storage vs Replicated Storage?

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

This discussion is preserved as it was. New answers, comments, and votes are closed.