TorNix~|nR Posted July 18, 2023 Share Posted July 18, 2023 Hello guys, I get this error when I use the convertNumber method attempt to perform arithmetic on local 'oldZcount' (a string value) function convertNumber ( number ) local formatted = number while true do formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2') if ( k==0 ) then break end end return formatted end --ADJUSTS PLAYERS ZOMBIE KILL SCORE function deanimated( ammo, attacker, weapon, bodypart ) if (attacker) then if (getElementType ( attacker ) == "player") and (getElementType ( source ) == "ped") then if (getElementData (source, "zombie") == true) then -- HERE -- local oldZcount = convertNumber(getElementData ( attacker, "Zombie kills" )) -- HERE -- if oldZcount ~= false then setElementData ( attacker, "Zombie kills", oldZcount+1 ) triggerEvent ( "onZombieWasted", source, attacker, weapon, bodypart ) else setElementData ( attacker, "Zombie kills", 1 ) triggerEvent ( "onZombieWasted", source, attacker, weapon, bodypart ) end end end end end addEventHandler("onPedWasted", resourceRoot, deanimated) any help please? Link to comment
Salchy Posted July 18, 2023 Share Posted July 18, 2023 You can try it: local oldZcount = convertNumber(getElementData(attacker, "Zombie kills") or "0") Link to comment
TorNix~|nR Posted July 18, 2023 Author Share Posted July 18, 2023 Already tried it, same problem Link to comment
Salchy Posted July 18, 2023 Share Posted July 18, 2023 so, you are saving a "string" value in that key. You should do a data type check. you could remaster the code, as it has some inconsistencies. Remember that the code is executed line by line, doing what we indicate, therefore, we must be logical and specific when programming. function deanimated( ammo, attacker, weapon, bodypart ) if (attacker) then if (getElementType ( attacker ) == "player") and (getElementType ( source ) == "ped") then if (getElementData (source, "zombie") == true) then local oldZcount = getElementData(attacker, "Zombie kills") or 0 -- get the value, if it has no value, by default, assign value 0 to the variable oldZcount if (type(oldZcount) ~= "number") then -- added to check the data type that is 'oldZcount', if different from number, (expected value) assign value 0 to the variable oldZcount oldZcount = 0 end setElementData(attacker, "Zombie kills", oldZcount + 1) triggerEvent("onZombieWasted", source, attacker, weapon, bodypart) end end end end it is not tested 1 Link to comment
TorNix~|nR Posted July 18, 2023 Author Share Posted July 18, 2023 Thank you for providing help but I tried this and multiple attempts, none of them worked any other help please? Link to comment
FlorinSzasz Posted July 18, 2023 Share Posted July 18, 2023 13 hours ago, TorNix~|nR said: Thank you for providing help but I tried this and multiple attempts, none of them worked any other help please? U can use -> tonumber() Link to comment
#!_A7M8D Posted July 26, 2023 Share Posted July 26, 2023 Hello function deanimated( ammo, attacker, weapon, bodypart ) if (attacker) then if (getElementType ( attacker ) == "player") and (getElementType ( source ) == "ped") then if (getElementData (source, "zombie") == true) then local oldZcount = getElementData ( attacker, "Zombie kills" ) if oldZcount ~= false then setElementData ( attacker, "Zombie kills", oldZcount+1 ) triggerEvent ( "onZombieWasted", source, attacker, weapon, bodypart ) else setElementData ( attacker, "Zombie kills", 1 ) triggerEvent ( "onZombieWasted", source, attacker, weapon, bodypart ) end end end end end addEventHandler("onPedWasted", root, deanimated) if you want to use convertNumber use it when showing the data not with setElementData. Cheers! Link to comment
ExMohmD Posted August 4, 2023 Share Posted August 4, 2023 The error you're encountering is due to the fact that the convertNumber function is returning a string instead of a number. In Lua, when you perform arithmetic operations on strings, you'll get this error. The convertNumber function seems to add commas as thousand separators to the input number. However, it doesn't actually convert the number to a numeric type. To fix this, you need to remove the commas from the string before using it in arithmetic operations. Here's an updated version of the convertNumber function that should solve the issue: function convertNumber(number) -- Remove commas from the number local cleanedNumber = string.gsub(number, ",", "") local formatted = cleanedNumber while true do formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", "%1,%2") if k == 0 then break end end return formatted end Now, your deanimated function should work as expected without the error. The convertNumber function will clean the input number, remove any commas, and then add commas as thousand separators before returning the formatted string. Just replace the existing convertNumber function with the updated version provided above. 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