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

How to find triple args with string.sub()?

Asked by 5 years ago

So I am trying to make a command bar, and one of the commands is !givecash <plr>, <amo>. Whenever you press the ReturnEnter it should fire the event. Now while there is no problem with that, I do have a problem finding the amount of cash they would like to give the player. It should like something like this in the command bar: !givecash suspectshot108 515000. So my question is how do I find the amount? I know I can find the player with string.sub() and amount with string.sub() and awhile that is working I am lost on a way to get the amount.

LocalScript: Some code is taken out

if(string.sub(CommandLine.Text,1,10)) == string.lower("!givecash ") then
                local target = game.Players:FindFirstChild(string.sub(CommandLine.Text,11))
                local ammount = string.sub(CommandLine.Text,12)
                print(target,ammount)
                game:GetService("ReplicatedStorage").AdminEvents["CE"]:FireServer(target)
        end

2 answers

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

you could add up the amount of characters from the command prefix to the player name, so you can use string.sub() to sub the string in that amount.

local message = "!givecash EXpodo1234ALT 1000" -- lets say this is the message

local totalsub = 11 -- 11 since the prefix is 11 characters
local players = {"EXpodo1234ALT"} -- maybe just say this was the array that :GetPlayers() returned
local subprefix = string.sub(message,totalsub)

for i,v in pairs(players) do
    if string.match(subprefix,v) then --  if it finds the plr name inside the string
        local amount = string.len(v) -- gets the amount of characters in the name 

        totalsub = totalsub + (amount+1) -- adds it up
        break -- breaks the loop
    end
end

local AmountOfMoney = string.sub(message,totalsub,100)

print(AmountOfMoney) -- prints "1000"
workspace.NumberValue.Value = workspace.NumberValue.Value + AmountOfMoney -- example

also why I did (amount+1) is because theres a space between the name and amount.

Finished Script

game.Players.PlayerAdded:Connect(function(player)
    player.Chatted:Connect(function(message)
        if string.match(message,"!givecash ") then
            local totalsub = 11
            local players = game.Players:GetPlayers()
            local subprefix = string.sub(message,totalsub)

            for i,v in pairs(players) do
                if string.match(subprefix,v.Name) then
                    local amount = string.len(v.Name)

                    totalsub = totalsub + (amount+1)
                    local amountofmoney = string.sub(message,totalsub,100)

                    v.leaderstats.Cash.Value = v.leaderstats.Cash.Value + amountofmoney -- if you had a leaderstats called "Cash"
                    break
                end
            end
        end
    end)
end)
sorry if the comments made it hard to read
0
Well I don't know that the total amount is 12; That was just a guess, but I do know the total sub all the way to 11<plr> namespace25 594 — 5y
0
12 is the amount of characters inside ":givemoney " User#23365 30 — 5y
0
if it was "!givecash " then it would be 11 characters User#23365 30 — 5y
0
You're mistaken, there are 11. 2 special + 9 reg characters, so 11. namespace25 594 — 5y
View all comments (5 more)
0
Yes, !givecash. That is the command I am going for. namespace25 594 — 5y
0
changed my answer to use the "!givecash " prefix User#23365 30 — 5y
0
Thanks namespace25 594 — 5y
0
also if your using :GetPlayers(), use v.Name or it'll error User#23365 30 — 5y
0
you can also use `string.find()` too, but then thats a different story User#23365 30 — 5y
Ad
Log in to vote
0
Answered by
xEmmalyx 285 Moderation Voter
5 years ago
Edited 5 years ago

I made a system for trading using the chat with gmatch. I pulled the core of my code and used a textbox as a "CommandLine"

I put an example on my ScriptingHelpers Place(Uncopylocked) Roblox Place

THIS WILL NOT WORK IN A LOCAL SCRIPT You need to setup an event

script.Parent.FocusLost:Connect(function()
    local words = {}
    local msg = script.Parent.Text

    for v in msg:gmatch("[^ ]+") do
        table.insert(words, v)
    end

    if #words == 3 then
        if words[1] == "!givecash" then
            if game.Players:FindFirstChild(words[2]) and tonumber(words[3]) then
                game.Players[words[2]].Amount.Value = game.Players[words[2]].Amount.Value + words[3]
                script.Parent.Text = ""
            end
        end
    end

end)
0
I like your answer but I need it to use string.sub() namespace25 594 — 5y
0
ah all good xEmmalyx 285 — 5y

Answer this question