Ad
Log in to vote
0

How to fix "Argument 1 missing or nil" when turning strings into ints without result being nil?

Asked by
Meqolo 78
6 months ago

So I'm tryna create an admin script. This error is occurring at Line 53. Using print() I have identified the issue which is that the tonumber() function is turning a string of numbers into nil. However, if I remove the tonumber() function from that line and leave it as a string I get the error "unable to cast string to int64".

This is meant to detect if a player is within a certain group, that group is being detected in the table "Settings.Banned".

----------------------------------------------------
--                   Settings                     --
----------------------------------------------------
local Settings = {}

Settings.ToolStorage = game:GetService("ServerStorage")
Settings.Moderators = {}        -- Mods                                 Format: {"Username:UserId", "Group:GroupId", "Group:GroupId:RankId"}
Settings.Admins = {}            -- Admins                               Format: {"Username:UserId", "Group:GroupId", "Group:GroupId:RankId"}
Settings.Owners = {}            -- Owners                               Format: {"Username:UserId"}
Settings.Creators = {}          -- Creators                             Format: {"Username:UserId"}
Settings.Muted = {}             -- Muted players                        Format: {"Username:UserId"}
Settings.AdminBlacklist = {}    -- Blacklisted from using admin         Format: {"Username:UserId", "Group:GroupId", "Group:GroupId:RankId"}
Settings.Banned = {"MeqoIo:497574391", "Group:497574391:255", "Group:2969826"}          -- Banned players                       Format: {"Username:UserId", "Group:GroupId", "Group:GroupId:RankId"}
Settings.Permissions = {}       -- Command Permissions                  Format: {"Command:AdminLevel"}
Settings.MusicList = {}         -- Music list                           Format: {"MusicId:Name"}

Settings.SaveAdmins = true      -- Saves admins using datastores
Settings.SaveBanned = true      -- Saves banned players using datastores

Settings.EnableTrello = false   -- Is trello used to save data?         NOTE: The Main Admin Trello will over-ride this setting for exploit protection.
Settings.PrimaryTrello = ""     -- Primary Trello Board                 Format: "BoardID"
Settings.TrelloAppkey = ""      -- Your trello appkey (DO NOT SHARE)    Link: https://trello.com/app-key
Settings.TrelloToken = ""       -- Your trello token (DO NOT SHARE)     Link: https://trello.com/1/authorize?expiration=never&name=Olo_Admin_Commands&key=APP-KEY

Settings.CreatorPowers = true   -- Used to give me (Meqolo) admin for debugging purposes
Settings.ChatCmds = true        -- Allows commands to be ran through the chat

Settings.ServerLockMessage = "Server currently locked"  -- Message sent when players kicked because the server is slocked
Settings.MaxLogs = "250"                                -- Amount of logs stored before deleting them.

Settings.Console = true         -- Allowes a console to be used when "~" is pressed

----------------------------------------------------
--               End of Settings                  --
----------------------------------------------------


local DSS = game:GetService("DataStoreService")
local Banlist = DSS:GetDataStore("Banlist")
local Modlist = DSS:GetDataStore("Modlist")
local Adminlist = DSS:GetDataStore("Adminlist")
local Ownerlist = DSS:GetDataStore("Ownerlist")


game.Players.PlayerAdded:Connect(function(plr)
    local pat = "(%a+):(%d+)"
    for i,v in pairs(Settings.Banned) do
        if string.sub(v,1,5) == "Group" then    
            local f = string.sub(v,7,string.len(v))
            print(f)
            f = tonumber(f)
            print(f)
            print(plr:IsInGroup(f))
            if string.sub(v,7,string.len(v)):gmatch("(%f+):(%d+)") then
                for ke,va in string.sub(v,7,string.len(v)):gmatch("(%d+):(%d+)") do
                    print(ke)
                    print(va)
                    if plr:GetRankInGroup(tonumber(ke)) == tonumber(va) then
                        plr:Kick("Banned")
                    end
                end
            else
                print(string.sub(v,7,string.len(v)))
                if plr:IsInGroup(tonumber(string.sub(v,7,string.len(v)))) then
                    plr:Kick("Banned")
                end
            end
        end 

        for key,val in v:gmatch(pat) do
            if plr.UserId == val then
                plr:Kick("Banned")
            end
        end
    end
    local Banned = Banlist:GetAsync(plr.UserId) or false
    if Banned == false then
    else
        plr:Kick("Banned")
    end
end)

The main block of code causing the error is:

            local f = string.sub(v,7,string.len(v))
            print(f)
            f = tonumber(f)
            print(f)
            print(plr:IsInGroup(f))

2 answers

Log in to vote
1
Answered by
mattscy 3199 Moderation Voter
6 months ago

Just check if f isn't nil after you use tonumber (any non-number strings used in tonumber will return nil):

if string.len(v) > 7 then
    local f = string.sub(v,7,string.len(v))
    print(f)
    f = tonumber(f)
    if f then
        print(f)
        print(plr:IsInGroup(f))
    end
end
0
I already said that f was returning nil Meqolo 78 — 6mo
0
Yeah, and after it does you use 'if f == nil then' to check if it is nil or not before trying to use it as the parameter in IsInGroup. mattscy 3199 — 6mo
Ad
Log in to vote
0
Answered by
Meqolo 78
6 months ago

Fixed it myself, just needed to turn f into a string first.

Answer this question