Creating a Simple Loop to Send Variable From Client To Server

I had a lot of trouble understanding how to pass variables and how to create loops so I decided to put a quick tutorial here for beginners. I am not an expert programmer and I’m sure this isn’t optimized or the best way etc. etc., but it works and if you are new to this I think this will help you. This is a death loop to determine if a player is dead then empty his inventory and create a locker where other players can loot his inventory. DISCLAIMER: I DON’T KNOW FOR CERTAIN THE SQL STATEMENTS WORK THIS IS A CONCEPTUAL DISCUSSION
CLIENT SIDE:

----DEAD PLAYER INVENTORY DROP

Citizen.CreateThread(function() --Required
    local wait = 5000 --wait 5 seconds and run loop
    local alive = 1 -- if player is alive I assigned a value of 1
    local deadcount = 0 -- create local variable to count times player is murdered for unique LockerId
    local inventorydropped = false -- He isn't dead he has not dropped inventory
    while true do -- will run the loop from moment player logins and never stops
        Citizen.Wait(wait) -- wait the defined 5 seconds before checking if player is alive
        local can_wait = false -- for end of script to avoid making a bunch of lockers
        local ped = PlayerPedId() -- how you get the player's information on client side
        local _player = ped -- a variable I plan to send elsewhere to keep track of the player
        if not inventorydropped then -- check if the player has dropped inventory
            if IsEntityDead(ped) then -- check if the player is dead
                alive = 0 -- if he is dead then alive goes from 1 to 0
                    if alive == 0 then -- redundant I don't remember why I did this may not need
                        deadcount = deadcount+1 -- increment the player has died count for unique locker id later
                        local count = deadcount -- change the variable to what I plan to send elsewhere
                        TriggerServerEvent("redemrp:playerDeaded", _player, count) --He dead tell the server to do stuff and the server side needs these two variable for whatever it does it will send the current _player and current count to the server to do stuff with
                        inventorydropped = true --dead men own no things inventory dropped
                    end -- end the alive == 0 loop
                can_wait = true --he dead so wait longer than 5 seconds is allowed
                inventorydropped = false -- reset his inventory drop cause when he respawns he will not have dropped again until he is killed again
                alive = 1 -- he is technically alive now, even though he is dead don't think about it to much it is for the next loop after he respawns
            end --end the dead player loop
        end --end the inventory dropped loop
        if can_wait == true then --we said to wait longer when he dies
            wait = 120000 --wait 2 minutes before checking his alive or dead status again he won't have any inventory to worry about within 2 minutes anyway
            can_wait = false --he is no longer dead go back to 5 second loops
        else
            wait = 5000 --he aint dead wait 5 seconds
        end -- close
    end -- close
end) -- end everything

SERVER SIDE:
Now that we have determined the player is dead on the current we pass those variable to the server to do stuff with. This is the stuff I did with it:

RegisterServerEvent("redemrp:playerDeaded")
AddEventHandler("redemrp:playerDeaded", function(ID,deadedcount,coords) --Received from client ID,deadedcount and coords
    local _source = source --Dealing with same player that initiated function
    TriggerEvent('redemrp:getPlayerFromId', _source, function(user)
        TriggerClientEvent("redemrp_notification:start", _source, "You are deaded", 2, "success") --troubleshooting notification
    local charid = user.getSessionVar("charid") --needed for data queries
    local identifier = user.get('identifier') --unique identifier in database
    local deadidentifier = identifier.."dead"..deadedcount --create new unique identifier for new locker
    local player_inventory =  Inventory[identifier .. "_" .. charid] --define player inventory from DB
    local ToAliveInv = {} -- assign the inventory to a table
        --Table Created Wait 3 seconds
        Wait(3000)

        TriggerClientEvent("redemrp_notification:start", _source, "You are still deaded", 2, "success")--troubleshoot message

        --Create ToDeadLocker
        local playerped = GetPlayerPed(_source) --get player id
        local deadcoords = coords --get player location

        MySQL.ready(function()
            data.createLocker("doctor_1" , -329.27, 800.93 ,117.55, nil) --create a test locker at a known coordinate for troubleshooting
            data.createLocker(deadidentifier, coords.x , coords.y , coords.z, nil) --create the locker where users inventory will be dumped
            data.updateLockers(-1) -- update locker for everyone
            end)
            Wait(3000)
            TriggerClientEvent("redemrp_notification:start", _source, "Dead Locker Created", 2, "success") --troubleshooting notification

        --Get from Inventory add to Locker
        if player_inventory[1] ~= nil then -- start an incrementing loop for the inventory table
            Wait(3000)
            for i,k in pairs(player_inventory) do --run the loop
                table.insert(ToAliveInv ,{name = k.getName(), amount = k.getAmount(), meta = k.getMeta()}) --add line to table for each inventory item in loop
            end
        end

        local JsonItemsInventory = json.encode(ToAliveInv) --convert the table to json
        MySQL.Async.execute('UPDATE user_locker SET items = @items WHERE identifier = @identifier AND charid = @charid', { --use jason created to put all player inventory in DB
            ['@identifier']  = deadidentifier,
            ['@charid']  = 0,
            ['@items'] = JsonItemsInventory
        }, function (rowsChanged)
            data.updateLockers(-1) -- update locker for everyone
        end)
        TriggerClientEvent("redemrp_notification:start", _source, "Inventory Should Have Transferred", 2, "success")
        --Now delete players Inventory
        data.removePlayerInventory(_source) --delete players inventory
        Wait(4500)
        TriggerClientEvent("redemrp_notification:start", _source, "DeadID="..deadidentifier.."DeathCoords: x="..coords.x.." y="..coords.y.." z=",coords.z, 5, "success") --troubleshoot if you get this message everything should have executed properly
    end)
end)

And that’s it. I hope this helps someone I know it would have helped me. If you find this information helpful please give it a like and I stream most nights while me and a friend teach ourselves how to code cool stuff at www.twitch.tv/ksuie please come by I am happy to answer questions while I stream, however sometimes I miss the chat stream while I am navigating through thousands of files so just be patient and I will respond as soon as I see your comment. I’m happy to help beginners and if you aren’t a beginner I probably can’t help you because I have only been doing this for a couple of months. Thanks for checking out my fancy and thorough tutorial.Preformatted text

1 Like

I can’t believe no one thanked you for this. This is extremely helpful for those just starting out. Thank you.