Jump to content

help | setElementData Problem!


scolen

Recommended Posts

I made a hud but it has a bug. For those who register and login, setElementData is not set. How to fix it ?

If the resource is restarted, the element data will be set and saved in the account data

 

Server Side


addEventHandler("onPlayerQuit", root, function(type)
    if type == "Quit" then 
        local health = getElementHealth(source)
        local armor = getPedArmor(source)
        local water = getElementData(source, "water")
        local food = getElementData(source, "food")
        
        local account = getPlayerAccount(source)
        setAccountData(account, "health", health)
        setAccountData(account, "armor", armor)
        setAccountData(account, "water", water)
        setAccountData(account, "food", food)
    end
end)


addEventHandler("onPlayerLogin", root, function(pa, ca)
   local account = getPlayerAccount(source)
   if not isGuestAccount(account) then 



       local health = getAccountData(account, "health")
       local armor = getAccountData(account, "armor")
       local water = getAccountData(account, "water")
       local food = getAccountData(account, "food")

       setElementHealth(source, health)
       setPlayerArmor(source, armor)
       setElementData(source, "water", water)
       setElementData(source, "food", food)
   end
end)


function drinkWater(player, price, size)
    if getPlayerMoney(player) < System.prices.food_price then 
       outputChatBox("water cannot be given because you have no money", player, 255, 0, 0)
       
    else
        if getElementData(player, "water") >= 95 then 
            outputChatBox("You can't get more food because your stomach is full!", player, 255, 0, 0)
        else
            setElementData(player, "water", getElementData(player, "water") + 20)
            takePlayerMoney(player, System.prices.water_price)
            triggerClientEvent(player, "drinking", player)
        end
    end
end

function eatFood(player, price, size)
    if getPlayerMoney(player) < System.prices.water_price then 
       outputChatBox("Food cannot be given because you have no money", player, 255, 0, 0)
       
    else
        if getElementData(player, "food") >= 95 then 
            outputChatBox("You can't get more food because your stomach is full!", player, 255, 0, 0)
        else
            setElementData(player, "food", getElementData(player, "food") + 20)
            takePlayerMoney(player, System.prices.food_price)
            triggerClientEvent(player, "eating", player)
        end
    end
end




addEventHandler("onPlayerWasted", root, function()
    setElementData(source, "water", 50)
    setElementData(source, "food", 50)
end)

addEvent("fireStop", true)
addEventHandler("fireStop", root, function(player)
    toggleControl(player, "fire", false)
end)

addEvent("fireStart", true)
addEventHandler("fireStart", root, function(player)
    toggleControl(player, "fire", true)
end)

addEvent("runStop", true)
addEventHandler("runStop", root, function(player)
    toggleControl(player, "sprint", false)
end)

addEvent("runStart", true)
addEventHandler("runStart", root, function(player)
    toggleControl(player, "sprint", true)
end)

Client Side 

local screenW, screenH = guiGetScreenSize()
setElementData(localPlayer, "water", 100)
setElementData(localPlayer, "food", 100)

showPlayerHudComponent("health", false)
showPlayerHudComponent("armour", false)
showPlayerHudComponent("money", false)
showPlayerHudComponent("ammo", false)
showPlayerHudComponent("weapon", false)
showPlayerHudComponent("clock", false)

function render()


    health = getElementHealth(localPlayer)
    armor = getPedArmor(localPlayer)
    water = getElementData(localPlayer, "water")
    food = getElementData(localPlayer, "food")
    money = getPlayerMoney(localPlayer)
    time = getRealTime()
    hour = time.hour
    minute = time.minute
    second = time.second

    weaponID = getPlayerWeapon(localPlayer)
    weaponName = getWeaponNameFromID(weaponID)
    ammo = getPedAmmoInClip(localPlayer)
    allammo =  getPedTotalAmmo(localPlayer)
 
    
    -- health
    dxDrawCircle((screenW - 225), (screenH / 5000 + 30), 25, -270,(health * 3.6 - 270), tocolor(0, 255, 0), 30) -- health line
    dxDrawCircle((screenW - 225), (screenH / 5000 + 30), 18, 0, 360, tocolor(26, 26, 26)) -- black circle
    dxDrawImage((screenW - 225) - (27 / 2), (screenH / 300 + 15), 27, 27, "assets/icons/heart.png") -- health icon

    -- armor
    dxDrawCircle((screenW - 160), (screenH / 5000 + 30), 25, -270,(armor * 3.6 - 270), tocolor(194, 194, 194), 30) -- armor line
    dxDrawCircle((screenW - 160), (screenH / 5000 + 30), 18, 0, 360, tocolor(26, 26, 26)) -- black circle
    dxDrawImage((screenW - 160) - (27 / 2), (screenH / 300 + 16), 27, 27, "assets/icons/armor.png") -- armor icon

    -- water
    dxDrawCircle((screenW - 95), (screenH / 5000 + 30), 25, -270,(water * 3.6 - 270), tocolor(10, 252, 236), 30) -- water line
    dxDrawCircle((screenW - 95), (screenH / 5000 + 30), 18, 0, 360, tocolor(26, 26, 26)) -- black circle
    dxDrawImage((screenW - 95) - (20 / 2), (screenH / 300 + 12), 20, 30, "assets/icons/water.png") -- water icon

    -- food
    dxDrawCircle((screenW - 30), (screenH / 5000 + 30), 25, -270,(food * 3.6 - 270), tocolor(209, 102, 8), 30) -- food line
    dxDrawCircle((screenW - 30), (screenH / 5000 + 30), 18, 0, 360, tocolor(26, 26, 26)) -- black circle
    dxDrawImage((screenW - 28) - (27 / 2), (screenH / 300 + 13), 27, 27, "assets/icons/food.png") -- food icon


    --background
    dxDrawRectangle((screenW - 235), (screenH / 5000 + 62), 260, 50, tocolor(23, 23, 23, 230))
    dxDrawCircle((screenW - 235), (screenH / 5000 + 87), 25, -90, -270, tocolor(23, 23, 23, 230))

    -- money 
    dxDrawText("MONEY: $"..money, (screenW - 203), (screenH / 5000 + 70), 20, 20 , tocolor(0, 186, 43), 0.5, "bankgothic")
    
    -- real time
    dxDrawText(string.format("TIME: %02d:%02d:%02d", hour,minute,second), (screenW - 200), (screenH / 5000 + 90), 20, 20 , tocolor(166, 166, 166), 0.5, "bankgothic")

    -- weapons
    if weaponID ~= 0 then
        dxDrawRectangle((screenW - 235), (screenH / 5000 + 120), 260, 25, tocolor(23, 23, 23, 230))
        dxDrawCircle((screenW - 235), (screenH / 5000 + 132.5), 12, -90, -270, tocolor(23, 23, 23, 230))
        dxDrawText(weaponName.." | "..ammo.." | "..allammo, (screenW - 203), (screenH / 5000 + 125), 20, 20 , tocolor(237, 186, 0), 0.5, "bankgothic") 
    end

  

    if (food <= 2) then 
        triggerServerEvent("fireStop", resourceRoot, localPlayer)
    else
        triggerServerEvent("fireStart", resourceRoot, localPlayer)
    end

    if (water <= 2) then 
        triggerServerEvent("runStop", resourceRoot, localPlayer)
    else
        triggerServerEvent("runStart", resourceRoot, localPlayer)
    end

end
addEventHandler("onClientRender", root, render)

setTimer(function(player)
    if (getElementData(localPlayer, "water") <= 2) then 
        return
    else
        setElementData(localPlayer, "water", getElementData(localPlayer, "water") - 1)
    end
end, System.decrease.water, 0)
setTimer(function(player)
    if (getElementData(localPlayer, "food") <= 2) then 
        return
    else
        setElementData(localPlayer, "food", getElementData(localPlayer, "food") - 1)
    end
end, System.decrease.food, 0)


 

Link to comment

It happens that when they register they have not played yet, therefore that data does not yet exist.
To solve it you can do it in two ways.
1. Force to start for the first time with elementData at 0

addEventHandler("onPlayerLogin", root, function(pa, ca)
   local account = getPlayerAccount(source)
   if not isGuestAccount(account) then 



       local health = getAccountData(account, "health") or 0
       local armor = getAccountData(account, "armor") or 0 
       local water = getAccountData(account, "water") or 0
       local food = getAccountData(account, "food") or 0

       setElementHealth(source, health)
       setPlayerArmor(source, armor)
       setElementData(source, "water", water)
       setElementData(source, "food", food)
   end
end)

or directly in the hud set it to 0 if you don't already have those elementData

function render()


    health = getElementHealth(localPlayer)
    armor = getPedArmor(localPlayer) 
    water = getElementData(localPlayer, "water") or 0
    food = getElementData(localPlayer, "food") or 0
    money = getPlayerMoney(localPlayer) 
    time = getRealTime()
    hour = time.hour
    minute = time.minute
    second = time.second

  ...

 

  • Thanks 1
Link to comment
1 hour ago, alex17&quot; said:

It happens that when they register they have not played yet, therefore that data does not yet exist.
To solve it you can do it in two ways.
1. Force to start for the first time with elementData at 0

addEventHandler("onPlayerLogin", root, function(pa, ca)
   local account = getPlayerAccount(source)
   if not isGuestAccount(account) then 



       local health = getAccountData(account, "health") or 0
       local armor = getAccountData(account, "armor") or 0 
       local water = getAccountData(account, "water") or 0
       local food = getAccountData(account, "food") or 0

       setElementHealth(source, health)
       setPlayerArmor(source, armor)
       setElementData(source, "water", water)
       setElementData(source, "food", food)
   end
end)

or directly in the hud set it to 0 if you don't already have those elementData

function render()


    health = getElementHealth(localPlayer)
    armor = getPedArmor(localPlayer) 
    water = getElementData(localPlayer, "water") or 0
    food = getElementData(localPlayer, "food") or 0
    money = getPlayerMoney(localPlayer) 
    time = getRealTime()
    hour = time.hour
    minute = time.minute
    second = time.second

  ...

 

(or) what will happen ?

Link to comment

Hmm, I see it's a modified version of a DayZ GM I've once posted, normally upon registration all data are set to a default value, 100 for food and water and 1000 for health ? If they're not it means you have a problem with the registration function, may I take a look?

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...