AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   How to check user's KD? (https://forums.alliedmods.net/showthread.php?t=329273)

Dexon 12-16-2020 16:37

How to check user's KD?
 
Hello, could anyone help in this?
I tried with the following:

Code:

public kd_check(id){
    ColorChat(id, "!tYour KD: !g%i", (get_user_frags(id)/get_user_deaths(id)))
}

Log:
Run time error 11: divide
[0] bounty.sma::kd_check (line 43)

iceeedr 12-16-2020 17:10

Re: How to check user's KD?
 
Check that the frag is >0 before dividing.

OciXCrom 12-16-2020 17:39

Re: How to check user's KD?
 
Deaths > 0*
Dividing zero is fine, dividing by zero is not.
Also, you need to use %f, not %i because the result will most likely be a floating point number, not an integer.

fysiks 12-16-2020 23:12

Re: How to check user's KD?
 
Both get_user_frags() and get_user_deaths() return integers so the result will be an integer (see "integer division" e.g. 10/3 = 3, 5/10 = 0, etc.). To get a proper value for the KDR, you need to first convert them to floating point values with float() and then use %f in your format string.

Note: Do the 0 deaths check before converting to a float (i.e. store the deaths in a variable first so you don't need to call it twice).

Dexon 12-17-2020 04:44

Re: How to check user's KD?
 
Thank you all for the answer, now I got this:
Code:

if(get_user_frags(id) > 0){
    ColorChat(id, "!tKD-d: !g%f", get_user_frags(id)/get_user_deaths(id))
}

But it shows the same error. :/

And im not sure with this, may you show me how you thought?
Quote:

you need to first convert them to floating point values with float()

+ARUKARI- 12-17-2020 04:50

Re: How to check user's KD?
 
1 / 1 = 1, 1 / 0 = ?

OciXCrom 12-17-2020 07:39

Re: How to check user's KD?
 
Code:
new iDeaths = get_user_deaths(id) if(iDeaths) {     ColorChat(id, "!tKD-d: !g%f", float(get_user_frags(id)) / float(get_user_deaths(id))) }

Dexon 12-17-2020 16:19

Re: How to check user's KD?
 
Thank you all, it's all fine. :crab:

But usually K/D counts even if user has 0 deaths.
So may anyone have an idea about:
if a user has 0 deaths, make the value to 1, but not the get_user_deaths, because it would just give the player +1 death in the scoreboard?

OciXCrom 12-17-2020 17:31

Re: How to check user's KD?
 
if(iDeaths) print your regular message
else print 1

?

Natsheh 12-17-2020 20:50

Re: How to check user's KD?
 
PHP Code:

public kd_check(id){
    
ColorChat(id"!tYour KD: !g%.2f", ( get_user_fragsid ) / floatmaxget_user_deathsid ), ) ) ) )


here you go.

also their method works fine the only problem it won't print in chat if the user has a 0 deaths.


All times are GMT -4. The time now is 14:12.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.