PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience

Add Chat Commands

Add Chat Commands

Jun 23 2018, 2:02 AM PST

Problem

You want to use the Chatted event so you can run code when you type something.

Solution

Use the Chatted event of Player.

game.Players.PlayerAdded:connect(function(pl)
	pl.Chatted:connect(function(msg)
		if msg == 'removehead' and pl.Character and pl.Character:FindFirstChild('Head') then
			pl.Character.Head:Destroy()
		end
	end)
end)

Discussion

First, we connect the PlayerAdded event and then we connect a new event, the Chatted event to the Player. This event will fire when the player chats something and passes its callback function with the message. We check if the message is “removehead”, and also if the player’s head exists. Then we proceed to remove the head.

This means when any player chats “removehead”, their head will be removed if it exists.

Tags:
  • chat
  • command