/givecash command - MySQL

Full-script

AddEventHandler('playerJoining', function()
 local _source = source
 local identifier = GetPlayerIdentifiers(_source)[1]
 local bank = 0
 local cash = 0
 local isRegistered = false
  MySQL.Async.fetchAll("SELECT * FROM economy WHERE identifier = @identifier", {
  ['@identifier'] = identifier
  }, function(result)
 if result[1] ~= nil then
   isRegistered = true
 else
   MySQL.Async.execute('INSERT INTO economy (identifier, bank, cash) VALUES (@identifier, @bank, @cash)',
   { ['@identifier'] = identifier, ['@bank'] = bank, ['@cash'] = cash }
   )
 end
  end)
end)

RegisterCommand("givecash", function(source, args, rawCommand)
local identifier = GetPlayerIdentifier(GetPlayerFromServerId(args[1]))
local setamount = tonumber(args[2])
if IsPlayerAceAllowed(source, "admincommands") then
MySQL.Async.execute('UPDATE economy SET cash = cash + @setamount WHERE identifier = @identifier')
end
end)

How far away am I from succeding?

hopefully this helps you out… You was not telling the mysql where to look, You was not feeding the identifier or the amount to set the balance to.

RegisterCommand("givecash", function(source, args, rawCommand)
    local identifier = GetPlayerIdentifier(GetPlayerFromServerId(args[1]))
    local setamount = tonumber(args[2])

    if IsPlayerAceAllowed(source, "admincommands") then
       MySQL.Async.execute('UPDATE economy SET cash= cash + @cash WHERE identifier=@identifier',{
            ['@identifier'] = identifier,   --send identifier that you get from the args.
            ['@cash'] = setamount,   --This is where you send the data from
            }, function(result)
            if result then
                -- put code here if you want something to happen if successful 
            else
                --if fail put what ever here..
            end
        end)
    end
end)
1 Like

Helps me out a lot @ixHal but I can’t find some workaround for “GetPlayerFromServerId” as it is a client-based native =D
Thanks

You are passing a arg from client to server, So the id you are typing in is already the persons server ID? If so then all you need to do is remove that :slight_smile:

1 Like

Yep you can use GET_PLAYER_IDENTIFIER - Cfx.re Docs
without getting the player aslong as the ID passed is a server id

1 Like

Thank you very much @ixHal it works perfectly! I didn’t know it works like that :open_mouth:

No worries at all good luck!

1 Like