Simple Hand To Command In Script Not Working?

Hello, developers! :smiley:
So I was making a script as to when a player who is ranked 10 or above in a group says "!handto (player username) all their items go into the backpack of the player they specified to hand it to.

However, my script is not working and nothing is displayed in the output???

Can someone aid me or provide me with some sort of reference or help? I can’t seem to find anything.

Thank you,
bulder251 :slight_smile:

Here is the code for reference:

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]

				local PlayerToHand = game.Players:FindFirstChild(NameOfPlayerToTakeItem)
				

				if PlayerToHand 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)
3 Likes

You shouldn’t keep connecting functions to your RemoteEvent.

Yeah if I did it the other way (connecting only 1 function) or more than 1 remote event, using same code, it wouldn’t work…

-- Note that this should be a server script

-- Services --

local Players = game:GetService("Players")

-- Constants --

local LocalPlayer = Players.LocalPlayer

local GROUP_ID = 10421757
local MIN_COMMAND_RANK = 10

-- PlayerAdded --

Players.PlayerAdded:Connect(function(Player)
	if Player:GetRankInGroup(GROUP_ID) >= MIN_COMMAND_RANK then -- We know the player can use commands
		Player.Chatted:Connect(function(Message: string)
			if string.find(Message, "^!") then -- We know this message is probably a command
				local MsgElements = string.split(Message, " ")

				local Command = MsgElements[1]
				local CommandArg1 = MsgElements[2]

				if Command == "!handto" then
					local PlayerToHandTo = Players:FindFirstChild(CommandArg1)
					if PlayerToHandTo and Player.Character then
						local TargetTool = Player.Character:FindFirstChildOfClass("Tool")
						if TargetTool then
							TargetTool.Parent = PlayerToHandTo.Backpack
						end
					end
				end
			end
		end)
	end
end)
3 Likes