Deltanic Posted December 19, 2009 Share Posted December 19, 2009 (edited) Some code in onPlayerWasted: local playerkills = getElementData ( source, "Kills" ) local playerdeaths = getElementData ( source, "Deaths" ) + 1 if playerkills == 0 then local playerratio = 1/playerdeaths tonumber( ("%.2f"):format(playerratio) ) setElementData ( source, "Ratio", playerratio ) else local playerratio = playerkills/playerdeaths tonumber( ("%.2f"):format(playerratio) ) setElementData ( source, "Ratio", playerratio ) end But, it returns when I died three times and I haven't killed still 0.333343, and I don't know where that four can come from. Is this script wrong, or am I doing it wrong? Edited December 20, 2009 by Guest Link to comment
robhol Posted December 19, 2009 Share Posted December 19, 2009 That's just not how tonumber works. Tonumber returns the "numberized" value, it doesn't change the variable you put in. Link to comment
Deltanic Posted December 19, 2009 Author Share Posted December 19, 2009 EDIT: Result after 3 deaths, 0 kills: 0.330000013 Code: local playerkills = getElementData ( source, "Kills" ) local playerdeaths = getElementData ( source, "Deaths" ) + 1 if playerkills == 0 then local playerratio = 1/playerdeaths setElementData ( source, "Ratio", tonumber( ("%.2f"):format(playerratio) ) else local playerratio = playerkills/playerdeaths setElementData ( source, "Ratio", tonumber( ("%.2f"):format(playerratio) ) end So the result is different, but it isn't what I want. What to do now? Link to comment
50p Posted December 20, 2009 Share Posted December 20, 2009 EDIT: Result after 3 deaths, 0 kills:0.330000013 Code: local playerkills = getElementData ( source, "Kills" ) local playerdeaths = getElementData ( source, "Deaths" ) + 1 if playerkills == 0 then local playerratio = 1/playerdeaths setElementData ( source, "Ratio", tonumber( ("%.2f"):format(playerratio) ) else local playerratio = playerkills/playerdeaths setElementData ( source, "Ratio", tonumber( ("%.2f"):format(playerratio) ) end So the result is different, but it isn't what I want. What to do now? What do you want if that's not what you want? Why do you use 1 when playerkills is equal to 0? What if playerdeaths is 0? You will fail. Link to comment
robhol Posted December 20, 2009 Share Posted December 20, 2009 What do you want if that's not what you want? Why do you use 1 when playerkills is equal to 0? What if playerdeaths is 0? You will fail. playerdeaths will never be 0 if he doesn't start out on a negative number (which is unlikely) since he increments it before any dangerous divisions. Link to comment
Deltanic Posted December 20, 2009 Author Share Posted December 20, 2009 What do you want if that's not what you want? Why do you use 1 when playerkills is equal to 0? What if playerdeaths is 0? You will fail. I use 1 / playerdeaths, because divide by 0 is impossible. But what you say is also true, the answer would be not what I want. But I changed it: 1/playerdeaths/2 Indeed playerdeaths would be never 0, because this is only called when the player dies. But that doesn't matter. My numbers still won't have 2 decimals. I still don't know what to do. Link to comment
MikeChip Posted December 20, 2009 Share Posted December 20, 2009 I think he wants a Kill per Death ratio... function getPlayerKDRatio(player) local kills, deaths = getElementData(player, "Kills"), getElementData(player, "Deaths") local ratio = kills/deaths if(kills > 0 and deaths <= 0) then return string.format("%.2f", kills) end return string.format("%.2f", ratio) end Link to comment
50p Posted December 20, 2009 Share Posted December 20, 2009 What do you want if that's not what you want? Why do you use 1 when playerkills is equal to 0? What if playerdeaths is 0? You will fail. playerdeaths will never be 0 if he doesn't start out on a negative number (which is unlikely) since he increments it before any dangerous divisions. Not necessarily. What do you want if that's not what you want? Why do you use 1 when playerkills is equal to 0? What if playerdeaths is 0? You will fail. I use 1 / playerdeaths, because divide by 0 is impossible. But what you say is also true, the answer would be not what I want. But I changed it: 1/playerdeaths/2 Indeed playerdeaths would be never 0, because this is only called when the player dies. But that doesn't matter. My numbers still won't have 2 decimals. I still don't know what to do. You can NOT divide by 0 but you can divide 0 by anything but 0, which will give you 0. Link to comment
robhol Posted December 20, 2009 Share Posted December 20, 2009 Not necessarily.. what? If you start with a non-negative number, say, 5, you divide by 6. If you start with 0, you divide by 1. The only way in which you can have a dangerous division operator is if you start with -1, becoming 0. And that's unlikely, since you can't have died -1 times in a game. Link to comment
50p Posted December 20, 2009 Share Posted December 20, 2009 But you can get invalid or unexpected results from getElementData since every resource can modify element's data especially such as "Deaths", "Kills", etc. therefore using names such as "myresource.deaths" is more suitable for element data and is safer. Link to comment
Deltanic Posted December 20, 2009 Author Share Posted December 20, 2009 string.format("%.2f", playerratio) AWESOME! Thank you Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now