Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to make an chat command which allow me to say kill/Pla instead of kill/Player1?

Asked by
RoJiyn 12
7 years ago
Edited 7 years ago

What string manipulation must i use? is it string.find? Thanks edit:I already know how to make a command that work with kill/Player1 but does not work with kill/p or kill/pla edit:forgot to put the scripts here is it:

--part of the scripts
local asset = "rbxassetid://"


local admins = {
    ["itachhi1713"] = 5 , ["Player1"] = 5
}

local bet = "/"
print("My Admin Initialised :D :D :D")


function FindPlayer(name)
    for _, player in pairs(game.Players:GetPlayers()) do
        if player.Name:lower() == name:lower() then
            return player
        end
    end
end


function OnChatted(message,player)
    if message:sub(1,5) == "kill"..bet and admins[player.Name] == 5 then
        local target = FindPlayer(message:sub(6))
        if target and target.Character then
            target.Character:BreakJoints()
        end
    end
0
string.sub I believe FiredDusk 1466 — 7y

1 answer

Log in to vote
1
Answered by
M39a9am3R 3210 Moderation Voter Community Moderator
7 years ago
Edited 3 years ago

What you will need to look for is string.sub() or just the :sub() method.

You would want an event to listen for if a admin speaks. I will leave the figuring out of admins and such up to you as there is no current attempt. Whenever the player says something the script will listen and run the code, we will be checking if they say kill/.

game.Players.PlayerAdded:connect(function(PlayerWhoJoined)
    PlayerWhoJoined.Chatted:connect(function(TheMessageTheySaid)
        if TheMessageTheySaid:sub(1,5):lower() == 'kill/' then
            --Read below.
        end
    end)
end)

Whenever the message is evaluated and put through the if statement, it will check the first five characters of what the user said. Which if I were to say "hello world" the sub(1,5) would result in the hello. We will be checking if the first five characters result in kill/ which of course with "hello world" they do not. What the script will also allow you to do is lowercase the string in that instance so that if you were to go "KILL/" in a fit of rage at someone, it will still be accepted as a respected result.

Now for the actual detection of the targeted player. We would want to create a function that would allow for the returning of a player if one with the name exists.

function GetPlayer(TheRemainingString)
    for _,Player in pairs(game.Players:GetPlayers()) do
        if Player.Name:sub(1,TheRemainingString:len()):lower() == TheRemainingString:lower() then
            return Player
        end
    end
    return nil
end

What the function will do, whenever called, is it will look through all the players in Players of game (this code is able to be broken if your game gets exploited, I recommend GetService()). The actual object for players in the for loop is under the variable "Player". It will take Player.Name and pretty much cut out the part of the name that was said. Say I were to have said 'play' and the one of the players was "OnlyTenChr", I cut out the "only" portion of the name and compare it to "play". Those do not match of course, so it will continue on until it finds "play" == "play" and return the actual player. Or the loop may continue on until it can not find the player in which case the function returns nil.

I will leave this question up to the asker to implement in their script until they provide a question with reasonable attempt at producing their own script.


Did this answer your question? If it did, do not forget to hit the accept answer button. If you have any questions, feel free to comment below and I will get back to you.
Ad

Answer this question