Im trying to make a script that's activated when a player chats, I know how to control triggers of strings = letters, but how would I trigger based on any given value: Example (not meant to be serious)
if string == ANYVALUE then
--Actions
end
Im trying to make a script that's activated when a player chats, I know how to control triggers of strings = letters, but how would I trigger based on any given value: Example (not meant to be serious)
if string == ANYVALUE then
--Actions
end
So you want to see if a string has ANY numbers in it?
One way to do this is use the tonumber function. This function turns any given string into a number, if the given string has non-numerical characters in it then the tonumber function will return nil, so it's suggested that you don't use this method.
local str = '420'
print(tonumber(str)) -->420
print(type(tonumber(str))) --> number
--If it's not soley a number
local str = 'Four20'
print(tonumber(str)) --> nil
print(type(tonumber(str))) --> nil
--[[
The 'type' function returns what datatype the given arguments are(:
type('') --> string
type({}) --> table
type(function() end) --> function
etc..
]]--
Another way to do this is to use the string.gmatch function. This function lets you use string patterns to return a matching string. If you use this function in a generic for, then you can use %d+ as a pattern detector.. this pattern detector gets any numbers - and the + operator gets grouped numbers(:
local str = 'H3llo my n4m3 15 G0u15t3m'
local matches = {}
for i in str:gmatch('%d+') do
table.insert(matches,i)
end
print(unpack(matches)) -->3 4 3 15 0 15 3
And if i'm going overkill on this and all you need is to see if a string is equal to a wanted number then just compare the string to a preset number variable using tostring
local num = 420
local str = '420'
if tonumber(str) == num then
--code
end
--And vise-versa(;
if tostring(num) == str then
--code
end
This discussion is preserved as it was. New answers, comments, and votes are closed.