#hunger and thirst

1 messages · Page 1 of 1 (latest)

gilded hazel
#

Hello, i need help with hunger and thirst, how can i do to player don´t die and the health only decrease in max to 10% and stop?
It's because in my server i have AFK jobs and everyone dies because hunger and thirst i want to stay with hunger and thirst but have to stop when i get only 10% of health. Someone can help me?

vital hollow
#

Hi. Check qb-hud ( most probably ) search for hunger and thirst and anything related to damage while thirsty or hungry. Anyhow Ill give a solution a bit later when I get to my pc.

gilded hazel
vital hollow
#

Alright, if you could hang on for a bit and Ill send you a full code which youll just copy and paste.

Just tell me, how default are your qb resources?

#

In about an hour or so. Stay tuned

vital hollow
#

Ill be on in a few minutes

gilded hazel
#

i think i found it

#

CreateThread(function()
while true do
Wait(1000)
if LocalPlayer.state['isLoggedIn'] then
if QBCore.Functions.GetPlayerData().metadata['hunger'] <= 0 or QBCore.Functions.GetPlayerData().metadata['thirst'] <= 0 then
local ped = PlayerPedId()
local currentHealth = GetEntityHealth(ped)
SetEntityHealth(ped, currentHealth - math.random(5, 10))
end
end
end
end)

#

qb-core/client/loops.lua ?

vital hollow
#

Based on your code, just copy and overwrite it with this:

CreateThread(function()
    while true do
        Wait(1000)
        if LocalPlayer.state['isLoggedIn'] then
            local ped = PlayerPedId()
            local playerData = QBCore.Functions.GetPlayerData()
            local hunger = playerData.metadata['hunger'] or 100
            local thirst = playerData.metadata['thirst'] or 100
            local currentHealth = GetEntityHealth(ped)

            if hunger <= 0 or thirst <= 0 then
                if currentHealth > 120 then
                    SetEntityHealth(ped, currentHealth - math.random(5, 10))
                end
            end
        end
    end
end)
#

This should make your idea what you are looking for

#

If hunger is 0 and thirst 0 , the health starts dropping to 10% , or in other words 20hp. Stays that way. The only way for them to die is to take damage from gunshot and other stuff.

#

Let me know if it works so we can mark this post as resolved.

gilded hazel
#

i will try

#

currentHealth > 120
i use in config the max health are 200
no problem?

vital hollow
#

200 = default max health

#

How does your config look , could you share a snippet ?

gilded hazel
#

@vital hollow is working! Thanks!

vital hollow
#

Good, glad we've done this. Enjoy and happy coding 🙂 👏

gilded hazel
#

one more think

vital hollow
#

Yes?

gilded hazel
#

if i want to change instead of 10% (20hp) for 50% (100hp) what i have to change? just to understand

vital hollow
#

I'll write you down a little table so you remember:

Health Limit 10 %             ----> currentHealth > 120
Health Limit 25%  -----> currentHealth > 150
Health Limit 50% -----> currentHealth > 150
Health Limit 75 % ----> currentHealth > 175
#

The formula for this would be :

healthLimit = 100 + (200 * DesiredPercentage)
gilded hazel
#

Thanks! now i understand

#

i will open another topic, if you have time to help me go there, you are the goat