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

Getting specific text from TextBox with multiple words?

Asked by
Vividex 162
6 years ago

Hello, if you saw my other question I'm making an admin GUI (basically type the commands in a textbox)

so ya, im not that good at scripting but im trying to learn qq I'm trying to do a command like "ws me 30" but how do I get the specific number?

Command.Changed:Connect(function(Change)
    if Command.Text == "ws" then -- Comand.Text is the the TextBox
    game.Players.LocalPlayer.Humanoid.Walkspeed = number -- This is where I'm confused, how do I get the number from the textbox? Like if I put 30, or 60, or any number
    end
end)

2 answers

Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

To get the values you wish to get, you can use pattern matching:

Seperate a string "ws player speed" into {"ws","player", "speed"}:

local arguments = {} -- table to hold values.
for k in Command.Text:gmatch("[^%s]+") do
    arguments[#arguments + 1] = k
end
-- if Command.Text == "ws me 50" then
-- the table arguments would be {"ws", "me", "50"}

-- if Command.Text == "Hello, world.  This is text." then
-- thje table would be {"Hello,", "world.", "This", "is", "text."}

The pattern "[^%s]+" means that match a set of one one or more characters that do not include a space.

"[]" means match any character inside this.

"^" inside of "[]" means match any characters BUT the following characters in these brackets.

"%s" means space, you may also use " " (an actual space) rather than "%s"

"+" means match one or more of the last item (in this case, the previous item was any item that is not a space).

So, to check and see if the command entered was "ws":

local arguments = {} -- table to hold values.
for k in Command.Text:gmatch("[^%s]+") do
    arguments[#arguments + 1] = k
end

if arguments[1] == "ws" then
    if argiments[2] == "me" then -- check if the player is performing it on themself.
        local speed = tonumber(arguments[3])
        if speed ~= nil -- method tonumber returns nil if it cannot convert the value to a string.
            game.Players.LocalPlayer.Character.Humanoid.Walkspeed = speed
        end
    end
end

You can learn more about pattern matching in Lua from the official documentation: https://www.lua.org/pil/20.2.html

Ad
Log in to vote
0
Answered by 6 years ago

Use substrings. What you're doing right now is seeing if the Text == "ws." In your case, the Text is "ws me 30", not "ws".

I haven't used substrings in Lua, but I feel it would go something along the lines of

if Command.Text.sub("ws",1,2) then

or something like that. Sorry If I'm not much help I haven't been on Scripting Helpers in years (I would be on Studio right now but it's not working..)

I'm not sure how to help you with the walkspeed part. It's something like that though

Answer this question