Simple Command Not Working

Hey, developers! :smiley:

I am making a command that whenever a player who is within the selected ranks of a group says !handto (partial player name) only the item that is being held is transferred to the player who was to be handed to.

The problem is this does not happen when I run this command in the chat and would like some feedback.

local GroupId = 10421757
local MinimumRankToUseCommand = 10
local Player = game.Players.LocalPlayer

game.Players.PlayerAdded:Connect(function(Player)
	if Player:GetRankInGroup(GroupId) >= MinimumRankToUseCommand then
		Player.Chatted:Connect(function(Message)
			local Words = string.split(Message, " ")

			if Words[1] == "!handto" then
				local NameOfPlayerToTakeItem = Words[2]:lower()
				local Recipient = nil

				for x,y in pairs(game.Players:GetChildren()) do
					local PlayerName = y.Name:lower()
					if PlayerName:find(NameOfPlayerToTakeItem) then
						Recipient = y
					end
				end


				if Recipient then
					game.ReplicatedStorage.GivePlayerItem.OnServerEvent:Connect(function(Player, PlayerName)
						local ToolToGive = Player.Character:FindFirstChildWhichIsA("Backpack")
						ToolToGive.Parent = game.Players[PlayerName].Backpack
					end)
				end
			end
		end)
	end
end)

Thanks!

Hey there! The reason that this isn’t working is that you’re doing it in a local script, which can only do code that affects the person that the script’s under. For a hand-to script, you need to have the part where it transfers the item from one person to another in a server script, otherwise, it won’t work. You can do this through the utilization of remote events, which I won’t go into detail here.

Here’s a link to a video that should help you with this. How to Script a Cafe on Roblox: #2 How to Script a Hand To Gui - YouTube

Hello!
The script was a server script in server-script-service… :smiley_cat:

What type of script even is this? There’s t hings taht are local only and things that are server sided.

The first thing I’ll say is to remove local Player = game.Players.LocalPlayer if this is a server script

Secondly, there’s no need for the OnServerEvent if this is server sided, just it so that if a recipient was found, it just finds a tool in the player’s character and parents it to the recipient.

Also PlayerName is local to the in pairs loop, so it’s going to try to get nil from Players, and you should have a check to see if the player even has a tool equipped.

Finally, you’re trying to find a thing in the player’s character that’s a backpack instead of a tool

I’d try this

local Players = game:GetService("Players")

local GroupId = 10421757
local MinimumRankToUseCommand = 10

Players.PlayerAdded:Connect(function(Player)
	if Player:GetRankInGroup(GroupId) >= MinimumRankToUseCommand then
		Player.Chatted:Connect(function(Message)
			if not Player.Character then
				return
			end
			local Words = string.split(Message, " ")

			if Words[1] == "!handto" then
				local NameOfPlayerToTakeItem = Words[2] and Words[2]:lower()
				if not NameOfPlayerToTakeItem then
					return
				end
				local Recipient = nil

				for _,plr in pairs(Players:GetPlayers()) do
					local PlayerName = plr.Name:lower()
					if not PlayerName:find(NameOfPlayerToTakeItem) then
						return
					end
					Recipient = plr
				end

				local ToolToGive = Player.Character:FindFirstChildOfClass("Tool")

				if not Recipient or not ToolToGive then
					return
				end
				
				ToolToGive.Parent = Recipient.Backpack
			end
		end)
	end
end)