Kill Death Ratio issue

Hello! I’m trying to make a KDR system. I only have 1 problem.

To make a KDR system, you need to divide the kills by the deaths, the only problem is, if a player had 18 kills, and 7 deaths, their KDR would be 2.571428571428571 you see the problem.

Instead of displaying that entire number, I only want to display 2.57. How would I go about doing this?

3 Likes

I’m not totally sure if my answer is valid but if you do this

KDRS = tostring(KDR)
KDR = tonumber(string.sub(KDRS,1,4)) 

It may work but if the kdr is higher than 9.999 it may require the 4 integer in the sub function to be + 1

I’d use the following:

KDR = tonumber(string.format("%.2f", kills / deaths))

Change the number in the string (%.2f) to how many decimals you want.

-- Example
"%.2f" with num 2.3456 => 2.34
"%.4f" with num 2.3456 => 2.3456
1 Like

the KDR is displayed as “nan”, this is the code I have for testing purposes:

local player = game.Players.LocalPlayer

local kills = player:WaitForChild("Lifetime Kills").Value
local deaths = player:WaitForChild("Lifetime Deaths").Value

while true do
	wait(.1)
	KDR = tonumber(string.format("%.2f", kills / deaths))
	
	print(KDR)
end

Strange. Would it be possible to print the Kills and Death values before the operation?

For me it works fine:

print(tonumber(string.format("%.2f", 100.234))) -- Prints 100.23

the first thing that comes to my head:

local KDR = math.floor((Kills / Deaths) * 100)/100
2 Likes

The reason why it’s printing back nan is cause that’s the term for “Not a Number”

This usually happens when you attempt to use a mathematical function on something that’s not supposed to be resulted back normally, like say “50 / 0” would be equal to nan

The KDR is likely NAN because your deaths are zero.

the first thing that comes to my head:

local KDR = math.floor((Kills / Deaths) * 100) / 100

This does NOT account for when deaths == 0.

As many others are saying, when requiring your deaths make sure to default it to 1 in these calculations.

This can be done by using:

math.max(player:WaitForChild("Lifetime Deaths").Value, 1)

This will only change the value for the calculation, and won’t change the actual stats of the player.

im not incredibly savvy when it comes to math but in most games when you have 0 deaths and a number of kills they just set your kd to how many kills you have. would it not make sense to do the same in this situation?
like (piggybacking off of @kiloe2’s code):

local Deaths = blah blah blah
local Kills = blah blah blah
local KDR = nil --(or 0)
while true do
    if (Kills/Deaths) ~= 0 then
        KDR = math.floor((Kills / Deaths) * 100) / 100
    else
        KDR = Kills
    end
end

(i wrote this on my phone please let me know if there are any syntax errors)
Edit: added a while true do loop

Yes, it does.

In my example:

Deaths will become current deaths OR 1, whichever is greater.

If deaths is 0, then it would load the value 1 instead (0 < 1)

Divide anything by itself and you will get the same value back.

2/1 = 1
12547/1 = 12547
x/1 = x

However, there is one issue in your example:

This is incorrect.

  1. Dividing any number (except 0) will never give 0. For example: 1/1e10 = 0.00000…01
    Only 0/x will result in 0.
  2. You are still dividing by zero. The issue was that IF deaths == 0, then DON’T divide by zero.

Dividing kills/deaths will only be 0 if kills is zero. Otherwise it will be a fraction. And as I made clear you are still dividing by deaths even though it may be zero.

I hope it made sense, I had a bit of a hard time structuring this reply as there were three intertwined issues.

ah okay that makes sense i appreciate you for letting me know about this i always assumed dividing a number by 0 always gets you 0.

1 Like

No worries!

If you wish to know more, read this: Dividing by Zero (mathsisfun.com)

1 Like