Jump to content

convertNumber


Recommended Posts

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

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

  • Like 1
Link to comment

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
  • 2 weeks later...

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...