botanist Posted December 19, 2009 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 Handling editor | Modloader
robhol Posted December 19, 2009 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. Do NOT PM ME for help unless invited. - New MTA Script Editor Scripting help "etiquette": understandable language, relevant code (ALL code if unsure), [Lua] tags, error messages with line numbers. Super simple stuff.
botanist Posted December 19, 2009 Author 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? Handling editor | Modloader
50p Posted December 20, 2009 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. - MTA Script Editor - Ask your scripting questions properly, please. - 50p's public resources - Meta.xml - what is it for? How is it possible LOL
robhol Posted December 20, 2009 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. Do NOT PM ME for help unless invited. - New MTA Script Editor Scripting help "etiquette": understandable language, relevant code (ALL code if unsure), [Lua] tags, error messages with line numbers. Super simple stuff.
botanist Posted December 20, 2009 Author 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. Handling editor | Modloader
R7flOq7KeSsU Posted December 20, 2009 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
50p Posted December 20, 2009 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. - MTA Script Editor - Ask your scripting questions properly, please. - 50p's public resources - Meta.xml - what is it for? How is it possible LOL
robhol Posted December 20, 2009 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. Do NOT PM ME for help unless invited. - New MTA Script Editor Scripting help "etiquette": understandable language, relevant code (ALL code if unsure), [Lua] tags, error messages with line numbers. Super simple stuff.
50p Posted December 20, 2009 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. - MTA Script Editor - Ask your scripting questions properly, please. - 50p's public resources - Meta.xml - what is it for? How is it possible LOL
botanist Posted December 20, 2009 Author Posted December 20, 2009 string.format("%.2f", playerratio) AWESOME! Thank you Handling editor | Modloader
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