Super easy quick question

Why isnt this working, just just get ‘nan’

local kills = game.Players.LocalPlayer.leaderstats.Kills.Value
local deaths = game.Players.LocalPlayer.leaderstats.Deaths.Value
local killRatio = kills / deaths

while true do
	wait()
	script.Parent.Text = "K/D Ratio:  "..killRatio
end
local kills = game.Players.LocalPlayer.leaderstats.Kills.Value or 0
local deaths = game.Players.LocalPlayer.leaderstats.Deaths.Value or 0

Edit: Also, why is it inside a loop? And don’t use wait() use task.wait(). The value of killRation won’t change what’s the point of using a while loop.

1 Like
while true do
	task.wait()
	local kills = game.Players.LocalPlayer:WaitForChild("leaderstats").Kills.Value
	local deaths = game.Players.LocalPlayer:WaitForChild("leaderstats").Deaths.Value
	local killRatio = kills/deaths
	script.Parent.Text = "K/D Ratio:  "..killRatio
end

Is anything else erroring after you use this?

2 Likes

That’s true, the variable should be redefined inside the loop

2 Likes

I agree with your script! :grinning:ㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤ

1 Like

Instead of using a loop, just listen for an event being fired:

local PlayerObject = game:GetService("Players").LocalPlayer
local Leaderstats = PlayerObject:WaitForChild("leaderstats")

local Kills = Leaderstats.Kills
local Deaths = Leaderstats.Deaths

local TextLabel = script.Parent

local function UpdateKD()
	TextLabel.Text = "K/D RAtion: " .. (Kills.Value or 0) / (Deaths.Value or 0)
end

Kills:GetPropertyChangedSignal("Value"):Connect(UpdateKD)
Deaths:GetPropertyChangedSignal("Value"):Connect(UpdateKD)
1 Like

You can’t divide a number by 0 (n / 0), which seems to occur any time the number of deaths is 0.

Though there is a pretty easy fix for this, which I have left down below. math.max limits the value of the first parameter to being greater than or equal to the value of the second parameter you give it.

local killRatio = kills / math.max(deaths, 1)

Additionally, you should use connections here because they will only update your K/D ratio when necessary. Plus, your are getting the value of both kills and deaths out of the loop you have which means they will both always be 0 all the time.

--/ Waiting for leaderstats folder to load into the game
local leaderstats = game:GetService('Players').LocalPlayer:WaitForChild('leaderstats')

--/ Waiting for Kills and Deaths objects to load into the game
local kills = leaderstats:WaitForChild('Kills')
local deaths = leaderstats:WaitForChild('Deaths')

--/ Updates the TextLabel's text to show the player's KD
local function UpdateKD()
    script.Parent.Text = "K/D Ratio:  "..(kills.Value / math.max(deaths.Value, 1))
end

--/ Updates KD when kills or deaths values change and upon loading into the game
UpdateKD()
kills:GetPropertyChangedSignal('Value'):Connect(UpdateKD)
deaths:GetPropertyChangedSignal('Value'):Connect(UpdateKD)
1 Like

You can divide by 0, you just can’t divide 0 by 0.
( n/0 when n is above 0 results in inf, and -inf when n is below 0)

I would only change the UpdateKD function to something like this

local function UpdateKD()
    local ratio = kills.Value / deaths.Value

    if ratio == math.huge then
        ratio = "∞"

    elseif kills.Value == 0 and deaths.Value == 0 then
        ratio = "N/A"
    end

    script.Parent.Text = "K/D Ratio:  "..ratio
end
1 Like

Thank you very much its working, hwever its giving it to like 10 significant figs, is there a way i can make it only like 2 decimal places?

Something like this

script.Parent.Text = string.format("K/D Ratio:  %.2f", kills.Value / math.max(deaths.Value, 1))
2 Likes