Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

How Would I Use String.Match To Improve My Chat Command Script?

Asked by 9 days ago

I've been looking for ways to improve my chat commands script and I came across string.match... I thought it would be useful in my case because instead of me having to type in the targeted player's full username and use find first child to look for something matching that, i could use it and type an abbreviation of the plr's name, and then have the script search for the closest matching plr in player service. But I don't know how to use it.

local Valid_Commands_Table = require(game:GetService("ServerStorage").ValidCommands)
local Permitted_Players = require(game:GetService("ServerStorage").PlayerNamesPermitted)
local ToWarnEvent = game:GetService("ReplicatedStorage").ToWarn
local Maximum_Amount_Of_Warns = 3
function Check(Plr)
    for _, Permitted_Player in pairs(Permitted_Players) do
        return Plr.Name == Permitted_Player
    end
end
game:GetService("Players").PlayerAdded:Connect(function(Player)
    local Warns_Num_Value = Instance.new("NumberValue")
    Warns_Num_Value .Parent = Player
    Warns_Num_Value.Name = "Warns"
    Player.Chatted:Connect(function(StringMsg)
        local SplitString = StringMsg:split(" ")
        if Check(Player) and SplitString[1] == Valid_Commands_Table[1] then --Kick Command
            local NameOfPlayer = SplitString[2]
            local ActualPlayer = game:GetService("Players"):FindFirstChild(NameOfPlayer)
            if ActualPlayer then
                local Reason = StringMsg:split(NameOfPlayer)[2]
                ActualPlayer:Kick(Reason)
            end
        elseif Check(Player) and SplitString[1] == Valid_Commands_Table[2] then
            local Name_Of_Player = SplitString[2]
            local PlayerToWarn = game:GetService("Players"):FindFirstChild(Name_Of_Player)
            local Reason = StringMsg:split(Name_Of_Player)[2]
            local WarnsInPlayer = PlayerToWarn:WaitForChild("Warns")
            if WarnsInPlayer then
                WarnsInPlayer.Value += 1
                ToWarnEvent:FireClient(PlayerToWarn)
                if WarnsInPlayer.Value >= 3 then
                    PlayerToWarn:Kick("Max Number Of Warns Reached, therefore you have been kicked from the server.")
                end
            end
        elseif Check(Player) and SplitString[1] == Valid_Commands_Table[3] then
            local Targets = SplitString[2]
            if Targets == "All" then
                for _, TargetedPlayer in pairs(game:GetService("Players"):GetChildren()) do
                    local Char_Of_Plr = TargetedPlayer.Character
                    local HumanoidOfTP = Char_Of_Plr.Humanoid
                    HumanoidOfTP:TakeDamage(HumanoidOfTP.MaxHealth)
                end
            else
                local NameOfTargetedPlr = SplitString[2]
                local ActualPlayer = game:GetService("Players"):FindFirstChild(NameOfTargetedPlr)
                if ActualPlayer then
                    ActualPlayer.Character.Humanoid:TakeDamage(ActualPlayer.Character.Humanoid.MaxHealth)
                end
            end
        end
    end)
end)

^ Chat Commands Script I Have.

1 answer

Log in to vote
1
Answered by
raid6n 2290 Moderation Voter Community Moderator
9 days ago

Instead of string.match, you can use string.sub. String.sub gets the substring of a string or a part of a string.

Here's an example:

local String = "Hello!"
local substring = string.sub(String, 1, 5) -- first 5 characters of the string
print(substring) -- "Hello"

With your code, we can get the splitstring and then loop through all the players and then detect if a substring of every player is the same thing as the substring.

If you're confused, I made a string which explains a few things:

local Name = SplitString[2] 
for i, v in pairs(game.Players:GetPlayers()) do -- loops through all the players
    if string.sub(v.Name, 1, #Name) == Name then -- detects if the substring of a player is equal to the split string
        print("The player name is " .. v.Name) -- if it works, it'll print something like: The player name is raid6n
    end
end
0
Thanks a lot for that! JeffTheEpicRobloxian 142 — 7d
Ad

Answer this question