Givetool command trying to find tools name

Hello, I am making a custom admin panel for my group. I am stuck on my last command to give tools. Can anyone help find the problem? It automatically finds name by putting it a few letters, I am trying to do the same with the tool part. Have ideas of how I can fix this?

Goal command usage example:

:givetool Alert Si

It finds the player name as AlertShamrock (Yes, using myself as an example.), it will add a “sign” for an example to the player inventory, in this case, it is AlertShamrock.

Before you ask, there will more than one tool, for this command.

Script:

    elseif string.sub(msg, 1, 10) == ":givetool " and speaker:GetRankInGroup(4371823) >= 120 then
	local Msg = (string.sub(msg, 11))
	
	for _, v in pairs(game.Players:GetPlayers()) do
	if Msg == v.Name:lower():sub(1, #Msg) then 
	for _, r in pairs(game.ServerStorage:GetChildren()) do
		if Msg == v.Name:lower():sub(1, #Msg) and r.Name:lower():sub(1, string.sub(#Msg:len() - v.Name:len())) then
			print(v.Name)
		 game.ServerStorage:FindFirstChild(r.Name):Clone().Parent = game.Players:FindFirstChild(v.Name).Backpack		
		end
		end

If have any questions, comments or concerns contact me below!

Regards,
AlertShamrock

Hello Shamrock. I use my custom admin commands in my own game and I have made a script for this. It seems you are going WAY out of your way for this one. Here are 2 methods I have made that could help you with yours.

Autofill Name Script:

function autofillName(plrSaid, given)
	if string.lower(given) == "me" then
		return {plrSaid}
	end
	
	if string.lower(given) == "all" then
		return game.Players:GetPlayers()
	end
	
	local plrTable = {}
	for i, plr in pairs(game.Players:GetPlayers()) do
		if string.lower(given) == string.lower(string.sub(plr.Name,1,string.len(given))) then
			table.insert(plrTable,#plrTable + 1, plr)
		end
	end
	
	return plrTable
end

Autofill Tool Script:

function autofillTool(given)	
	local toolTable = {}
	
	if string.lower(given) == "all" then
		-- For loop finding all the tools directly in Server Storage -- 
		for i, tool in pairs(game.ServerStorage:GetChildren()) do
			if tool:IsA("Tool") then
				table.insert(toolTable,#toolTable + 1, tool)
			end
		end
		
		return toolTable
	end
	
	-- Finding the tools with the starting letters of the name -- 
	for i, tool in pairs(game.ServerStorage:GetChildren()) do
		if tool:IsA("Tool") then
			if string.sub(string.lower(tool.Name),1,string.len(given)) == string.lower(given) then
				table.insert(toolTable,#toolTable + 1, tool)
			end
		end
	end
	
	return toolTable
end

Given would be the name or the tool you are trying to find.

Again, I hope this helped. Feel free to reply if you have more questions!

1 Like

Thank you, I will look into this to see how I can fix my script!

Just remember, that returns a table to wherever you called it. It will contain the tool object itself. You just need a for loop to loop through the table and clone the value in each slot to the player’s inventory.

1 Like