Splitting the rest of the text

Hello developers!
im making a admin commands were if the user typed !message something here it will message to everyone something here. but the problem is I’m trying to split it and it ended up like this something. The rest of the words we’re not added.

Script:

-- Module script
local RunningCommands = {}
function RunningCommands.MessageWorld(player,UserMessage)
	local GlobalMessage = Instance.new("Message")
	GlobalMessage.Text = UserMessage
	GlobalMessage.Parent = Workspace
	local GlobalHint = Instance.new("Hint")
	GlobalHint.Text = "Message by "..player.Name
	GlobalHint.Parent = Workspace
	wait(5)
	GlobalMessage:Destroy()
	GlobalHint:Destroy()
end
return RunningCommands
-- script
if string.find(message, SettingsModule.Prefix.."message") or string.find(message, SettingsModule.Prefix.."msg") or string.find(message, SettingsModule.Prefix.."m") then
	local messagesplit = message:split(' ')
	local Command = messagesplit[1]
	local UserMessage = messagesplit[2]
	CommandsModule.MessageWorld(player,UserMessage)
end

Thanks for the help! i appreciate it :grinning:

replace this line of code with this:

local UserMessage = ""
for word in pairs(messageSplit) do
    if word != Command then
        UserMessage = UserMessage..word.." "
    end
end

UserMessage = UserMessage:sub(1, -2) -- this will take off the leading space
1 Like

When you split the string, it split all the words in the UserMessage too.

string.sub(message,#command+2,#message) fixes this issue by ignoring the command and only capturing the UserMessage. message is the original message, #command+2 is the number of characters in the command, and +2 was added to account for the space and the first letter of the message. #message is the total number of characters in message.

if string.find(message, SettingsModule.Prefix.."message") or string.find(message, SettingsModule.Prefix.."msg") or string.find(message, SettingsModule.Prefix.."m") then
	local messagesplit = message:split(' ')
	local Command = messagesplit[1]
	local UserMessage = string.sub(message,#command+2,#message)
	CommandsModule.MessageWorld(player,UserMessage)
end
1 Like

This guy did it better than me, use his method instead of mine. I was trying to look for something that would work similar to his but I couldn’t find anything so I did it differently.

2 Likes

it works but it turns to integer or the place of the splitting word. but thanks for the help!

thank you so much it finally worked!

1 Like

Sorry I just realized there were issues with the code, I forgot how to use pairs() while writing that code.

for index, word in pairs(messageSplit) do
    if word != Command then
        UserMessage = UserMessage..word.." "
    end
end

UserMessage = UserMessage:sub(1, -2) -- this will take off the leading space

This code should actually work.