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

Pay command producing an additional space?

Asked by
gitrog 326 Moderation Voter
7 years ago

So I have this pay command, and it doesn't work. It seems to be checking for an additional space after the player name, which is the problem, but I don't know how to fix it.

Here's the script

local recipientPattern = ".+%s" --Pattern for recipient of money
local amountPattern = "%d+" --Pattern for amount of money

function onChatted(message, player)
    if message:sub(1,5) == "/pay " then
        local recipient = message:sub(6):match(recipientPattern);
        recipient = recipient:reverse():sub(1):reverse() --omit whitespace
        local amount = message:sub(recipient:len()+6):match(amountPattern)
        if game.Players[recipient] == nil then
            print("Recipent: " .. recipient .. " not found!")
        else
            local sendMoney = game.Players[recipient.Name]
            print(sendMoney)
            if player.leaderstats.Money.Value >= amount then
                player.leaderstats.Money.Value = player.leaderstats.Money.Value - amount
                sendMoney.leaderstats.Money.Value = sendMoney.leaderstats.Money.Value + amount
            else
                print("Invalid money!")
            end
        end
    end
end

game.Players.PlayerAdded:connect(function(player)
    player.Chatted:connect(function(message) onChatted(message, player) end)
end)

Here's the error

Recipent: Player1  not found!
13:01:02.238 - Player1  is not a valid member of Players
0
On line 12, don't do ".Name". FiredDusk 1466 — 7y

Answer this question