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

How to use string.sub to detect a space for an admin script?

Asked by
trecept 367 Moderation Voter
6 years ago

Hi, I'm making my own admin script and currently it's going okay. With string.sub, I can make commands such as ;kill player, but I can't make commands that need more than 1 argument, like ;tp player player.

if string.sub(chat, 1, 4) == (";tp ") then
for i,player in pairs(plr(string.sub(chat, 5))) do

This is part of one of my commands, the "plr" function gets the player (e.g j for John). I'm using string.sub for chat (when the player chats) to see when the player says ";tp " and then getting the player from whatever they type after that (string.sub(chat, 5)). But if I want to put in a second player, how do I do it? The only way I know how is to do something like string.sub(chat, 5, 8) and then the next player string.sub(chat, 9) or something like that, but that would mean if the player enters any more than 3 letters for the player's name for the first player to be teleported it would break.

So with using string.sub, how do I detect when a player puts like a space, and then to get the player after that space? Or any other solution that works? Thank you!

2 answers

Log in to vote
0
Answered by
tantec 305 Moderation Voter
6 years ago

What I would use is a for loop to loop through all the letters in the message and to check the space where each name is

Player = game.Players.LocalPlayer

Player.Chatted:connect(function(msg)
-- checks if he wants to teleport
if msg:sub(1,4):lower() == ":tp " then

-- variables
number = nil
firstplayer = nil
secondplayer = nil

-- the for i runs through the msg using i as a savepoint for each letter
for i = 5, msg:len() do
if msg:sub(i,i) == " " then
number = i
break
end
end

-- checking that the number is even there
if number == nil then
print("Number Not There")
return
end

-- finds the player using sub and the sector where you intend the player's name to be
for i,v in pairs(game.Players:GetPlayers()) do
if v.Name:sub(1, number - 5):lower() == msg:sub(5, number - 1):lower() then
firstplayer = v
end
end

-- finds the player at the end of the msg so like :tp player player2 it's finding the player2 part
for i,v in pairs(game.Players:GetPlayers()) do
if v.Name:sub(1, msg:len() - number):lower() == msg:sub(number + 1):lower() then
secondplayer = v
end
end

-- checking if they exist
if firstplayer ~= nil and secondplayer ~= nil then
firstplayer.Character.HumanoidRootPart.CFrame = secondplayer.Character.HumanoidRootPart.CFrame
print(firstplayer.Name.." tp'ed to "..secondplayer.Name)
end

end
end)

Ad
Log in to vote
0
Answered by
Validark 1580 Snack Break Moderation Voter
6 years ago

Try something like this?

local COMMAND_PREFIX = (";"):byte()

local function GetCommandArguments(Command)
    local Arguments = {}
    local CommandName
    if Command:byte() == COMMAND_PREFIX then
        CommandName = Command:match("^.(%l+)")
        for Arg in (Command:gsub("^.%l+%s*", "")):gmatch("%w+") do
            table.insert(Arguments, Arg)
        end
    end
    return CommandName, Arguments
end

table.foreach(select(2, GetCommandArguments(";kill player player2")), print)

Answer this question