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

How to check if it's a number value?

Asked by 5 years ago
Edited 5 years ago

So for instance, i have this:

game:GetService("Players").randomplayer.Chatted:Connect(function(msg)
       if msg == "range" then

end
end)

How would i check if after the range, it's a number value and what number it is?

I think it's something to do with using dots like "range" .. something i don't know though..

0
don't use :connect, its deprecated, use :Connect instead cmgtotalyawesome 1418 — 5y
0
ok, but like that doesn't help me here? ATestAccount420 31 — 5y
0
if tonumber(msg) then User#25115 0 — 5y
0
ok then, how do i store the value i get ATestAccount420 31 — 5y
View all comments (13 more)
0
local num = tonumber(msg); if num then User#25115 0 — 5y
0
you could use string.sub and convert to unicode/ascii and check if it's within the range 48 to 57. turtle2004 167 — 5y
0
also, tonumber() does not check if it's a number, it just converts a string to a number. So if they don't say a number it will cause an error. turtle2004 167 — 5y
0
local playerId = "1234567890" local newPlayerId = tonumber(playerId) print(newPlayerId) >>>123456789 turtle2004 167 — 5y
0
how would i apply that to my script, im sort of stupid ATestAccount420 31 — 5y
0
Lol, you're not stupid. I'll write something up in a minute turtle2004 167 — 5y
0
thank you my good sir ATestAccount420 31 — 5y
0
Tbh, unicode might be unnecessary, so i'm just checking turtle2004 167 — 5y
0
Alright ATestAccount420 31 — 5y
0
cant u just use `if tonumber(string) then` User#23365 30 — 5y
0
it'll return nil if its not a number User#23365 30 — 5y
0
if i do that it says attempt to compare number with nil ATestAccount420 31 — 5y
0
if tonumber("hello") then -- false, if tonumber("1") then -- truthy User#23365 30 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

i would use string.sub

game:GetService("Players").randomplayer.Chatted:Connect(function(msg)
       if string.sub(msg, 1, 5) == "range" then
    local num = tonumber(string.sub(msg, 6,#msg))
end
end)

also .. is used for "concatenating" string together

print("Hello " .. "World")
>Hello World
Ad

Answer this question