Issue with detecting chat

Sorry I didn’t know how to word the title but I want a script where you could do for a example /giveitem itemname. But I don’t know how to get the name of the item, this is what I’ve tried so far

game.Players.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(msg)
		local character = Player.Character or Player.CharacterAdded:Wait()
		if (Player.Name == "snobW0lf") then
			if msg == "Giveitem "..game.ServerStorage:FindFirstChildWhichIsA("Tool").Name then
				
				print(msg)
			
				
			end


		end
	end)
end)

Something like this should work:

game.Players.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(msg)
		local character = Player.Character or Player.CharacterAdded:Wait()
		if (Player.UserId == 210901995) then -- Check to see if its you, also we should use player Id because user names can change
			if msg:match("/giveitem") then -- check if /giveitem is in the message
				local itemName = msg:split(' ')[2] -- get the second thing (or the thing after the space, in our case the item name)
                local tool = game.ServerStorage:FindFirstChild(itemName) -- Get the tool
                if tool then -- Make sure it exists
                    -- Give the tool
                    local clone = tool:Clone() -- clone the tool
                    clone.Parent = player.Backpack -- give it to the player
                end
			end
		end
	end)
end)
2 Likes

I’m just going to post a more customizable version of @domboss37’s solution:

--//Services
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")

--//Tables
local Admins = {
	210901995,
}

--//Functions
Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(message)
		if not player.Character then
			return
		end
		
		if message:match("/giveitem") and table.find(Admins, player.UserId) then
			local itemName = message:split(" ")[2]
			
			if not itemName then
				return
			end
			
			local Tool = ServerStorage:FindFirstChild(itemName)
			
			if not Tool then
				return
			end
			
			local newTool = Tool:Clone()
			newTool.Parent = player.Backpack
		end
	end)
end)
2 Likes