How can I make a !Code #### Command in chat

How can I make a chat command when players say for example !Code Hi
it gives them stats points or money.

If it’s even possible it would be great if you guys can explain it to me.

Thank you!

local CodeHiAmount = 40 --How much you want the code to give


game.Players.PlayerAdded:connect(function(player) --When a player joins assign them them as a variable player
	leaderstats = Instance.new("Model", player) --Adds a new model into the player
	leaderstats.Name = "leaderstats" --names it leader stats. (This allows it to work properly)

	Points = Instance.new("IntValue", leaderstats)  --Adds a statistic
	Points.Name = "Points" --You can name this gems or coins, whichever you'd like.
	Points.Value = 0  --Makes how much they have 0

	player.Chatted:connect(function(msg) --When the player speaks assign their message to a variable.
		if msg == "!Code Hi" then --If the message is "!Code Hi" then
			Points.Value = Points.Value + CodeHiAmount --How many points you have is your points + how much you want to give.
		end
	end)
	
	
end)

This should work.

A way to do this would be to use string.split() and search for your prefix/command modifier.

For example:

local prefix = "!code" -- Sets your code prefix
local Players = game:GetService("Players") -- References Roblox Players

Players.PlayerAdded:Connect(function(player) -- When a new player joins
     player.Chatted:Connect(function(message) -- When that player chats
          local split = string.split(string.lower(message), " ") -- Split the string into chunks
          if (split[1] == prefix) then -- If the first chunk equals the prefix then
               if (split[2] == "YOUR CODE HERE") then -- If the second chunk equals a command then
                    --split[3] would be the next word in the command and so on
                    print(split[3])
               elseif (split[2] == "hi") then
                    print(split[3])
               end
          end
     end)
end)

Running !code hi 20 would print

20

Keep in mind a code can only be one word! (This is also not case sensitive)

Instance.new(“Object”, Parent) shouldn’t be used for performance reasons, Parent should be the last property set.

local Part = Instance.new("Part")
Part.Parent = workspace

Apologies, I’m not the best scripter.

Sorry for the late response but where does this go and is it a server script or local?

That all goes in a Server Script in ServerScriptService

Ok bet do u have discord just incase i have any questions?

You can message me on the Devforum DMs