I’ve been working on a very basic inventory script and breathalyzer script for RP. They both function but if you put in text other than a number for a playerid it errors out, and I don’t know how to fix that.
I’m not posting the entire script here yet, but this is the part that will error out if the input is not a number
if (args[1] == “/bactest”) then
CancelEvent()
if args[2] ~= nil then
local id = tonumber(args[2])
local bac2 = bac[id]
if bac2 ~= nil then
TriggerClientEvent(‘chatMessage’, source, “Breathalyzer”, {47, 121, 239}, "This user’s BAC is: "…bac2)
else
TriggerClientEvent(‘chatMessage’, source, “Breathalyzer”, {47, 121, 239}, “This user does not have a BAC set.”)
end
else
TriggerClientEvent(‘chatMessage’, source, “Breathalyzer”, {47, 121, 239}, “Usage: /bactest [player id]”)
end
end
if type(variable) == "number" then
end
1 Like
The args[] array is the input from a chat message, would that be a string making that statement always false?
I’ll give it a try though
You can use tonumber() and check if the return is nil or not.
Edit: Just realized you already use tonumber(). Just check if it is nil. As @Frazzle just posted.
1 Like
Try this
if args[1] == "/bactest" then
CancelEvent()
if args[2] ~= nil then
if tonumber(args[2]) ~= nil then
if GetPlayerName(tonumber(args[2])) then
if bac[tonumber(args[2])] then
TriggerClientEvent("chatMessage", source, "Breathalyzer", {47, 121, 239}, "This users BAC is: "..bac[tonumber(args[2])])
else
TriggerClientEvent("chatMessage", source, "Breathalyzer", {47, 121, 239}, "This user does not have a BAC set.")
end
else
TriggerClientEvent("chatMessage", source, "Breathalyzer", {47, 121, 239}, "Usage: /bactest [player id]")
end
else
TriggerClientEvent("chatMessage", source, "Breathalyzer", {47, 121, 239}, "Usage: /bactest [player id]")
end
else
TriggerClientEvent("chatMessage", source, "Breathalyzer", {47, 121, 239}, "Usage: /bactest [player id]")
end
end
2 Likes