I am currently creating a simple script that is meant to take the message/command “/sm” and then whatever you want to put after that, and then put the text after the command into a Message instance and display it for 5 seconds and then delete it.
My problem is the fact that I don’t know how to get the following text after “/sm” and store it in a variable, and then put it in the message, so I need help with that. If you can, edit it in your own way to make it work how it is above this paragraph.
My current code is:
local chattedText
game.Players.PlayerAdded:Connect(function(player)
player.Chatted:Connect(function(msg)
if msg == "/sm " + chattedText then
local serverMessage = Instance.new("Message")
serverMessage.Parent = workspace
serverMessage.Name = player.Name + "'s ServerMessage"
serverMessage.Text = chattedText
wait(5)
serverMessage:Destroy()
end
end)
end)
Use the string.split() function to split the message into a table containing each argument. Then with that table, filter out the prefix + command and you’ll have your arguments to use in the command.
A more simple solution would be to simple subtract the amount of characters which the command takes up, and use the remaining string as your message (which in this case would probably be easier). You can do this by using string.gsub().
I need to know how to gain the text the player enters after the command.
e.g. “/sm Sample text.”
In this case, I would need to know how to get “Sample text.”, as the script doesn’t know what it would be otherwise. Then, I would need to know how to put that text inside of the Message instance.
I’ve provided you with two methods you could possibly use. You will have to manipulate the msg string using said methods in order to get the text you want from the message.
And yes, it would be better to use a TextLabel instead. As @dollychun stated, Message instances are deprecated and TextLabels should be favored for newer work instead. With TextLabels you also have more customization features such as RichText, Fonts, and scaling - all of which is not possible with a Message instance.
Remember that with a TextLabel you will need to filter any sort of text that a player provides to display to the server, as TextLabels are not filtered automatically.