Jump to content

[Help]Random Keys


MRXBBC

Recommended Posts

function generateString ( len )
    
    if tonumber ( len ) then
        math.randomseed ( getTickCount () )

        local str = ""
        for i = 1, len do
            local charlist = allowed[math.random ( 1, 3 )]
            str = str .. string.char ( math.random ( charlist[1], charlist[2] ) )
        end

        return str
    end
    
    return false
    
end

function test (source)
	Key = generateString(12)
	outputChatBox ( "Generated string was: " .. Key, root, 0, 255, 0 )
end
addCommandHandler("test",teste )

Hello Community, I would like some help with the following code, I would like the generated string to be saved in a table, and then checked if it exists or not

Edited by MRXBBC
Link to comment
  • Moderators

I don't understand exactly what you want, but:

local KEYS = {}

local string = generateString(12)

KEYS[string] = true -- save string to table

if KEYS[string] then -- check, is string in the table
    print("Key exists in table!")
end

 

Edited by Patrick
  • Thanks 1
Link to comment
33 minutes ago, Patrick said:

I don't understand exactly what you want, but:


local KEYS = {}

local string = generateString(12)

KEYS[string] = true -- save string to table

if KEYS[string] then -- check, is string in the table
    print("Key exists in table!")
end

 

I did so, I would like the player to use the command together with the generated key

function teste (source)
    KEYS[Key] = true
	Key = generateString(12)
	outputChatBox ( "Generated string was: " .. Key, root, 0, 255, 0 )
	

end
addCommandHandler("teste",teste )


addCommandHandler("Gen",
function(keyuser)
	if KEYS[Key] then
		outputChatBox ( "You Key: " .. Keys[Key], root, 0, 255, 0 )
	else
		outputChatBox ( "not", root, 0, 255, 0 )
end
end)

 

Edited by MRXBBC
Link to comment
local data = { Keys = { } , PlayerKey = { } } ; 

local allowed = { { 48, 57 }, { 65, 90 }, { 97, 122 } }
function generateString ( len )
    
    if tonumber ( len ) then
        math.randomseed ( getTickCount () )

        local str = ""
        for i = 1, len do
            local charlist = allowed[math.random ( 1, 3 )]
            str = str .. string.char ( math.random ( charlist[1], charlist[2] ) )
        end

        return str
    end
    
    return false
    
end

addCommandHandler ( "teste" , function ( player ) 
local Key = generateString(12)
data.Keys[Key] = true; 
data.PlayerKey[player] = Key;
outputChatBox("Your Code : "..tostring(Key),player)
end ) ;

addCommandHandler ( "Gen" , function ( player ) 
if ( data.PlayerKey[player] ) then 
outputChatBox("Your Key : "..tostring(data.PlayerKey[player]),player,0,255,0,true)
else 
outputChatBox("You don't have key",player,255,0,0,true)
end 
end ) ; 

 

Edited by -Ilker.
  • Thanks 1
Link to comment
17 minutes ago, -Ilker. said:

local data = { Keys = { } , PlayerKey = { } } ; 

function generateString ( len )
    
    if tonumber ( len ) then
        math.randomseed ( getTickCount () )

        local str = ""
        for i = 1, len do
            local charlist = allowed[math.random ( 1, 3 )]
            str = str .. string.char ( math.random ( charlist[1], charlist[2] ) )
        end

        return str
    end
    
    return false
    
end

addCommandHandler ( "teste" , function ( player ) 
local Key = generateString(12)
data.Keys[Key] = true; 
data.PlayerKey[player] = Key;
outputChatBox("Your Code : "..tostring(Key),player)
end ) ;

addCommandHandler ( "Gen" , function ( player ) 
if ( data.PlayerKey[player] ) then 
outputChatBox("Your Key : "..tostring(data.PlayerKey[player]),player,0,255,0,true)
else 
outputChatBox("You don't have key",player,255,0,0,true)
end 
end ) ; 

 

 

 

 

is this just for the player who generated a key? or can any other player use it?

Link to comment
local data = { Keys = { } } ; 

local allowed = { { 48, 57 }, { 65, 90 }, { 97, 122 } }
function generateString ( len )
    
    if tonumber ( len ) then
        math.randomseed ( getTickCount () )

        local str = ""
        for i = 1, len do
            local charlist = allowed[math.random ( 1, 3 )]
            str = str .. string.char ( math.random ( charlist[1], charlist[2] ) )
        end

        return str
    end
    
    return false
    
end

addCommandHandler ( "teste" , function (  ) 
local Key = generateString(12)
data.Keys[Key] = true; 
outputChatBox(tostring(Key).." this is a new key",root)
end ) ;

addCommandHandler ( "Gen" , function (  ) 
if ( data.Keys ) then 
outputChatBox("You Key : "..tostring(data.Keys),root,0,255,0,true)
else 
outputChatBox("not",root,255,0,0,true)
end 
end ) ; 

 

  • Thanks 1
Link to comment
Just now, -Ilker. said:

local data = { Keys = { } } ; 

local allowed = { { 48, 57 }, { 65, 90 }, { 97, 122 } }
function generateString ( len )
    
    if tonumber ( len ) then
        math.randomseed ( getTickCount () )

        local str = ""
        for i = 1, len do
            local charlist = allowed[math.random ( 1, 3 )]
            str = str .. string.char ( math.random ( charlist[1], charlist[2] ) )
        end

        return str
    end
    
    return false
    
end

addCommandHandler ( "teste" , function (  ) 
local Key = generateString(12)
data.Keys[Key] = true; 
outputChatBox(tostring(Key).." this is a new key",root)
end ) ;

addCommandHandler ( "Gen" , function (  ) 
if ( data.Keys ) then 
outputChatBox("You Key : "..tostring(data.Keys),root,0,255,0,true)
else 
outputChatBox("not",root,255,0,0,true)
end 
end ) ; 

 

thanx bro

Link to comment
6 minutes ago, MRXBBC said:

 

returned You Key: table: 0x55ecae718cf0

 

yeah i'm sorry , you can use this 

value = nil

local allowed = { { 48, 57 }, { 65, 90 }, { 97, 122 } }
function generateString ( len )
    
    if tonumber ( len ) then
        math.randomseed ( getTickCount () )

        local str = ""
        for i = 1, len do
            local charlist = allowed[math.random ( 1, 3 )]
            str = str .. string.char ( math.random ( charlist[1], charlist[2] ) )
        end

        return str
    end
    
    return false
    
end

addCommandHandler ( "teste" , function ( player ) 
local Key = generateString(12)
value = Key
outputChatBox(Key.." this is a new key",root)
end ) ;

addCommandHandler ( "Gen" , function ( player ) 
if ( value ~= nil ) then 
outputChatBox("You Key : "..tostring(value),root,0,255,0,true)
else 
outputChatBox("not",player,255,0,0,true)
end 
end ) ; 

 

  • Thanks 1
Link to comment
On 28/04/2020 at 16:50, -Ilker. said:

yeah i'm sorry , you can use this 


value = nil

local allowed = { { 48, 57 }, { 65, 90 }, { 97, 122 } }
function generateString ( len )
    
    if tonumber ( len ) then
        math.randomseed ( getTickCount () )

        local str = ""
        for i = 1, len do
            local charlist = allowed[math.random ( 1, 3 )]
            str = str .. string.char ( math.random ( charlist[1], charlist[2] ) )
        end

        return str
    end
    
    return false
    
end

addCommandHandler ( "teste" , function ( player ) 
local Key = generateString(12)
value = Key
outputChatBox(Key.." this is a new key",root)
end ) ;

addCommandHandler ( "Gen" , function ( player ) 
if ( value ~= nil ) then 
outputChatBox("You Key : "..tostring(value),root,0,255,0,true)
else 
outputChatBox("not",player,255,0,0,true)
end 
end ) ; 

 

 

Oops, brother, I implemented the code this way, but I wanted to define a validity for the keys, would you?

--server--
value = nil

local allowed = { { 48, 57 }, { 65, 90 }, { 97, 122 } }
function generateString ( len )
    
    if tonumber ( len ) then
        math.randomseed ( getTickCount () )

        local str = ""
        for i = 1, len do
            local charlist = allowed[math.random ( 1, 3 )]
            str = str .. string.char ( math.random ( charlist[1], charlist[2] ) )
        end

        return str
    end
    
    return false
    
end

function Teste (  ) 
local accName = getAccountName ( getPlayerAccount ( source ) ) 
if isObjectInACLGroup ("user."..accName, aclGetGroup ( "Console" ) ) then
local Key = generateString(12)
value = Key
outputChatBox(Key.."New Key",root)
else
outputChatBox("Out",root)	
end
end
addEvent ("Gen:Set", true)
addEventHandler ("Gen:Set", root, Teste)



addCommandHandler ( "User" , function ( player ) 
if ( value ~= nil ) then 
outputChatBox("Your Key: "..tostring(value),source,0,255,0,true)
setElementData ( player, "Vip", "Unlock" )
else 
outputChatBox("key does not exist",player,255,0,0,true)
end 
end ) ; 

 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...