Turning a string into an integer

So I’m working with the chat right now and I’m wondering how I can convert strings into integers, no, I don’t mean like this tonumber(String). I don’t really know how to explain it but I’ve been trying to figure it out for hours. Here’s the code:

if string.sub(Message, 1, 10) == "/giverank " then
	local Player2 = game.Players:FindFirstChild(string.sub(Message, 8))
	local Value = tonumber(string.sub(Message, string.len(Player2)))
    if Value ~= nil and Value >= 1 then
        Player.Rank.Value = Player.Rank.Value + Value
    end

Here’s the output:

04:53:00.572 - ServerScriptService.ChatScript:460: invalid argument #1 to 'len' (string expected, got nil) -- erroring at `string.len(Player2)`
04:53:00.573 - Stack Begin
04:53:00.575 - Script 'ServerScriptService.ChatScript', Line 460
04:53:00.576 - Stack End
1 Like

The problem here is that Player2 is a Player Object, not a string.

You can avoid this by doing Player2.Name

It doesn’t work. As I said, I’ve been trying for hours. Here’s the output:

05:19:21.800 - ServerScriptService.ChatScript:460: attempt to index nil with 'Name'
05:19:21.801 - Stack Begin
05:19:21.802 - Script 'ServerScriptSerivce.ChatScript', Line 460
05:19:21.802 - Stack End

That means Player2 doesn’t exist. Let me look over your code again and I’ll get back to you. Ok, so this seems to me like the message is not working for string.sub. May I ask what this is attempting to do?

Well I’m trying to make it so when you do something like “/giverank BLAHBLAHBLAH 2” it will add rank to the persons already existing rank instead of overwriting the rank.

Well, it seems you are trying to remove 8, if I read correctly, are you typing 8?

I don’t get what you mean. I’m typing the otherplayers name and then I’m typing how many ranks I want to add.

Yes. I’m looking at the string.sub line. What exactly are you trying to accomplish there? I do not use it very often.

1 Like

The issue is that FindFirstChild is returning nil. Assuming Player2’s Name should be string.sub(Message, 8), the start position for string.sub would be off because the 8th character would be n. (they typed "/giverank " before the player’s name)


However, i’d just recommend using pattern matching instead of using sub.

local Player2, Value = string.match(Message,  "/giverank%s-([%w%p]+)%s+(%d+)")
3 Likes

@DexFried, I would recommend you using string.split(), because it’s easier to get around with.

Don’t use string.sub(), try doing this: local messageSplit = string.split(Message, " "). It’s going to split the message by space characters.
The “messageSplit” is a table, that means you should do if messageSplit[1] == "/giverank" then instead of if string.sub(Message, 1, 10) == "/giverank " then.
If you would switch to string.split(), you need to know that messageSplit[2] should be player name in your case and messageSplit[2] should contain number.

But what would you do if the player would type /giverank<multiple spaces>player 123 ?
messageSplit[2] would be then invalid.

Additionally, you could do something like this:
message = message:gsub("%s+"," ") but that would not work for every command. (For example /announcement or /message)


However the best and the most efficient solution (just as @Jaycbee05 said) are string patterns.

Maybe string patterns are the best, but there is no problem when somebody types lots of spaces on the chat, because Roblox automatically replaces them to one space. Try it in games.

No it does not. Roblox returns the original unformatted and unfiltered message to you.

I meant that it does replace the spaces (you see on the chat?). However, it returns a string with spaces, but still- it doesn’t error at all.

But I don’t care what it says in the chat. I care about what string i get in the return.
And yes, it doesn’t error. Why would it error?
I am saying that if you’d split a text with multiple spaces next to eachother you wouldn’t get the same results as you would expect.

I interpreted it like it would error, sorry.

I am using string.split(), because I would tell that this is a chat command: /giverank oof 20, not this: /giverank oof<multiple spaces>20.

1 Like