So, I am making this admin script where everything goes very well. My only problem is that I can't seem to choose which strings I want to read. I've used stuff like message:sub(4, 6) to get different characters, but when it comes to finding the player name entered I can't determine how long the player name will be. I need this information to get the strings coming behind the character name. An example is: /warn WaterFoox "REASON". I want to get the reason for why they got warned and put it into a frame that warns them. I've read on the wikis, but I don't really understand the explanation of whats listed here: https://developer.roblox.com/articles/Lua-Libraries/string#string.sub
Here are some of my code:
game.Players.PlayerAdded:Connect(function(plr) plr.Chatted:Connect(function(message) for i, admin in pairs(Admins) do if plr.Name == admin then message = string.lower(message) if message:sub(1, 6) == "/warn " then for _,player in pairs(game:GetService("Players"):GetPlayers()) do if message:sub(7) == player.Name:sub(1,message:sub(7):len()):lower() then if player then local TheWarn = Warn:Clone() TheWarn.Frame.Frame.MSGTEXT.Message.Text = message:sub(8) TheWarn.Parent = player.PlayerGui TheWarn.Frame:TweenPosition(UDim2.new(0.34, 0,0.362, 0), "Out", "Quint", 3) wait(10) TheWarn.Frame:TweenPosition(UDim2.new(0.34, 0,1, 0), "Out", "Quint", 3) print("Warned " .. player.Name) end end end end end end) end)
I would appreciate an explanation of what I need to use and please comment anything I should change in my script. I'll be happy with everything I get!
Thanks in advance!
~WaterFoox
Hello there!
You might want to look into string patterns and how they work, there is a wonderful wiki tutorial on the wiki page or you can search up some youtube videos on them.
One video I highly recommend is String Patterns Tutorial where this guy explains in-depth how you can manipulate strings for that reason. (Watch Part 2 aswell)
However, I'll provide a basic example for getting the username here using basic string patterns which even works with usernames with numbers.
local command = '/warn 0WaterFo0x Exploiting' local username = (string.match(command, '/warn%s(%w+)')) local reason = (string.match(command, '/warn%s%w+%s(%w+)')) print("Username: " .. username, '\nReason: ' .. reason)