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

Why Command with arguments doesn't work?

Hello I need help beacuse I don't know why this script isn't working.

game:GetService("Players").PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(message)
		local split = message:split(" ")
		if split[1] == "/give" then
			local arg1 = split[2]
			local arg2 = split[3]
			local arg3 = split[4]

			if arg1 and arg2 and arg3 then
				local player = game.Players..split[2]
				local tool = game.ReplicatedStorage..split[3]..split[4]
				
				tool:Clone(player.Backpack)
			end
		end
	end)
end)

2 answers

I found the problem. there is script that works.

game:GetService("Players").PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(message)
		local split = {}
		for word in string.gmatch(message, "%S+") do
			table.insert(split, word)
		end

		if split[1] == "/give" then
			local arg1 = split[2]
			local arg2 = split[3]
			local arg3 = split[4]

			if arg1 and arg2 and arg3 then
				local player = game.Players:FindFirstChild(arg1)
				local tool = game.ReplicatedStorage:FindFirstChild(arg2):FindFirstChild(arg3)

				tool:Clone().Parent = player.Backpack
			end
		end
	end)
end)

There are a couple of issues with the given code:

Fixing the player retrieval issue in the code.

local player = game.Players..split[2] is attempting to concatenate the Players service with the element at index 2 of the split array. This is not allowed in Luau, and would result in a runtime error. To retrieve the player with the specified name, you should use the GetPlayerByName method of the Players service, like this:

local player = game:GetService("Players"):FindFirstChild(split[2])

Fixing the tool retrieval issue in the code.

local tool = game.ReplicatedStorage..split[3]..split[4] is attempting to concatenate the ReplicatedStorage service with the element at index 3 of the split array, and then concatenate the result with the element at index 4 of the split array. This would also result in a runtime error. To retrieve the tool from ReplicatedStorage, you should use indexing, like this:

local tool = game:GetService("ReplicatedStorage")[split[3]][split[4]]

These changes should fix the issues with the code and allow it to work as intended.

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