How would I make a :Giveto admin command

What I am trying to do is make a Admin command where if you have an item in your inventory then you say :Giveto (Player) and it would give them that tool. Here is what I have, and what I think is correct.

game.Players.PlayerAdded:Connect(function(plr)
plr.Chatted:Connect(function(msg)
local message = string.sub(msg, 1, 9)
if message == ":Giveto " then return end
local player_name = string.sub(msg, 9)
assert(game.Players:FindFirstChild(player_name))
local player = game.Players:FindFirstChild(player_name)
local char = player.Character or player.CharacterAdded:Wait()

end)

end)

Please help me try to figure out how to make it. Thank you

Is this for a specific tool? Or just any item in the inventory, or what they’re holding?

What they are holding like I was holding a Hamburger. I would say :Giveto TheeDeathCaster and then you would have it in your inventory.

Alright, that’s fairly simple lol. You could use the FindFirstChildOfClass method and see if there’s a Tool in the speaker’s character. If so, it could be parented to the target player’s Backpack.
For example,

local tool = plr.Character:FindFirstChildOfClass("Tool")
if tool == nil then
    return
end
tool.Parent = player.Backpack

EDIT
I just noticed in your code, your conditional’s checking if the player said :Giveto , and if so to return. Did you mean to use ~= instead of ==?

game.Players.PlayerAdded:Connect(function(plr)
plr.Chatted:Connect(function(msg)
local message = string.sub(msg, 1, 9)
if message == ":Giveto " then return end
local player_name = string.sub(msg, 9)
assert(game.Players:FindFirstChild(player_name))
local player = game.Players:FindFirstChild(player_name)
local char = player.Character or player.CharacterAdded:Wait()
local tool = plr.Character:FindFirstChildOfClass(“Tool”)
if tool == nil then
return
end
tool.Parent = player.Backpack

end)

end)

Would it be like this? I am not the best scripter

1 Like

Yeah, although you might want to add a check for the speaker’s character, just in case it’s nil.
The local char = player.Character or player.CharacterAdded:Wait() isn’t needed either.
EDIT
Rewrote the script to make it look a smidge more readable.

if message:sub(1, 8):lower() == ":giveto " and speaker.Character ~= nil then -- ":giveto " is 8 characters long, checks for the speaker's character
	local target = game.Players:FindFirstChild(message:sub(9)) -- Get the target
	local tool = speaker.Character:FindFirstChildOfClass("Tool") -- Get the tool
	if target == nil or tool == nil then -- Is the target or tool nil?
		return -- Return, stopping execution
	end
	tool.Parent = target.Backpack -- Add tool to target's backpack
end

You could just have a Player.Chatted event and when it happens get the player’s character, set the tools parent to the input username.