[HELP] Is value an Int, float or string?

Hi guys I’m having trouble working out if an input value is an integer, float or string.
Simple right? Just use things like string.find(result, “str”), etc.

Well the problem I’m having is that the input value is always being sent through as a string before I can find what it is, no matter what the value is because the value is the result of player input using an onscreen keyboard that manipulates values used by the scripts, so ofcourse I need to determine what the value is before I let it change the value so that it doesn’t break the script.

This is kinda hard to explain. So long story short:

  • Value is being received as a string, no matter what the value is.
  • I need to figure out if value is actually an int, str or float and then convert value accordingly.

I know this is probably a really simple thing to do, I’m pretty much self taught so there’s loads of gaps in my learning, which I learned from my last post.

Here’s the on screen keyboard, which is incomplete, wrong and always finds a string, but I guess it gives an example of what I am trying to achieve.

function osk(tx, example, lengh, typ)
	AddTextEntry('FMMC_KEY_TIP1', tx)
	DisplayOnscreenKeyboard(1, "FMMC_KEY_TIP1", "", example, "", "", "", lengh)
	blockinput = true 

	while UpdateOnscreenKeyboard() ~= 1 and UpdateOnscreenKeyboard() ~= 2 do 
		Citizen.Wait(0)
	end
		
	if UpdateOnscreenKeyboard() ~= 2 then
		local result = GetOnscreenKeyboardResult()
		local strfind = string.find(result, "str" )
		local go = false
		if typ == "text" then go = true end
		if typ == "int" then 
			notify(type(result))
			if type(result) ~= "string" then
				local tn = isInt(result)
				notify(tn)
				if tn == true then
					go = true 
				else
					result = tonumber(string.format("%.0f", result))
					go = true
					notify("~o~Caution~w~:~n~Value is a ~r~FLOAT~w~.~n~~y~Rounded ~w~value to an ~g~Integer~w~.")
				end
			else 
				notify("~r~ERROR~w~:~n~input needs to be a ~p~INTEGER~c~(No decimal)~w~. ~n~Example: ~o~100~w~.")
			end
		end
		if typ == "float" then 
			--Do the same as int^ but float lol
		end
		if go == true then
			Citizen.Wait(500)
			blockinput = false
			
			return result
		end
	else
		Citizen.Wait(500) 
		blockinput = false 
		var.mu.osk = false
		return nil
	end
end

Here’s what happens when I put a int in something that is meant to be am int:

Changing a string with a string works fine, obviously.

Since Float isn’t basic type of values in Lua, all numerical values are considered as number, you could use something like:

local function returnStringType(str) 
    if tonumber(str) ~= nil then
        if tonumber(str) % 1 == 0 then
            print('integer')
        else
            print('float')
        end
    else
        print('string')
    end
end
1 Like

Thank you dude :smiley: