Jump to content

strange problem


Castillo

Recommended Posts

hey, i've got a strange problem updating a gui label with a data i get from server side O_o, here is my code and the error i'm genting.

client:

function upClientStats(zombiekills,deaths) 
if deaths == 0 then 
guiSetText(ratioLabel, "Ratio: N/A") 
else 
ratio = (tonumber(zombiekills)/tonumber(deaths))*10 
guiSetText(ratioLabel, "Ratio: "..tostring(ratio).." %") 
end 
end 
addEvent("updateClientStatsGui", true) 
addEventHandler("updateClientStatsGui", getRootElement(), upClientStats) 

server:

function info2(cliente) 
local account = getPlayerAccount (cliente); 
if not isGuestAccount(account) then 
local zombiekills = getAccountData(account,"zombieKills") 
local deaths = getAccountData(account,"deaths") 
triggerClientEvent (cliente,"updateClientStatsGui",cliente,zombiekills,deaths) 
    end 
end 
addEvent("getInfoFromClient", true) 
addEventHandler("getInfoFromClient", getRootElement(), info2) 

Error: attempt to perfrom arithmetic on a nil value its at zombiekills if i'm right.

Thanks in advance

Link to comment

if player didnt kill any zombie and nothing is in his elementData, you are probably passing nil

this probably work:

triggerClientEvent (cliente,"updateClientStatsGui",cliente,zombiekills or "0",deaths or "0") 

if not:

  
local zombiekills = getAccountData(account,"zombieKills") 
if (not zombiekills) then zombiekills=0 end 
  

same for deaths

Link to comment

i know, now explain me - what's wrong with my solution?

on this line you are doing arithmetic operation

  
ratio = (tonumber(zombiekills)/tonumber(deaths))*10 
  

error says it cant do arithmetics on nil value

so it means one/both of them (zombiekills or deaths) are arithmetic.

you are triggering it from server.

syntax is ok, so the only problem is data that is send from server

i suggested you to fix it.

you said something that i didnt understand..

try little debug.

  
function upClientStats(zombiekills,deaths) 
  outputDebugString("got values: "..tostring(zombiekills).." and "..tostring(deaths)) 
  if deaths == 0 then 
    guiSetText(ratioLabel, "Ratio: N/A") 
  else 
    ratio = (tonumber(zombiekills)/tonumber(deaths))*10 
    guiSetText(ratioLabel, "Ratio: "..tostring(ratio).." %") 
  end 
end 
addEvent("updateClientStatsGui", true) 
addEventHandler("updateClientStatsGui", getRootElement(), upClientStats) 
  

  
function info2(cliente) 
  local account = getPlayerAccount (cliente); 
  if not isGuestAccount(account) then 
    local zombiekills = getAccountData(account,"zombieKills") 
    if (not zombiekills) then zombiekills=0 end 
    local deaths = getAccountData(account,"deaths") 
    if (not deaths) then deaths=0 end 
    outputDebugString("sending values: "..tostring(zombiekills).." and "..deaths) 
    triggerClientEvent (cliente,"updateClientStatsGui",cliente,zombiekills,deaths) 
  end 
end 
addEvent("getInfoFromClient", true) 
addEventHandler("getInfoFromClient", getRootElement(), info2) 
  

Link to comment
Are you sure that "zombieKills" can be processed to a number?

Remember that "zombieKills" is saved at your account, maybe is causing problems your account's zombie kills data (it's a boolean value? or a nil maybe?). Just an idea.

I don't get what do you mean with this...

Link to comment
Solid, check my replies - ive already add "protection" not to send boolean/nil, but zero.

also check what debug is displaying..

i have added what you said and keeps saying attempt to perfrom arithmetic on a nil value

also when i open the menu it says this: got values: nil and nil, sending values: 966 and 0, got values: 966 and 0

Edit: i was thinking that i can also make it server side then trigger it and use guiSetText but the problem now is that this keeps saying my ratio are my kills...

local rat = 2 
  
function round(num, idp) 
    local mult = 10^(idp or 0) 
    return math.floor(num * mult + 0.5) / mult 
end 
  
function ratio(cliente) 
local account = getPlayerAccount (cliente) 
local zombiekills = getAccountData(account,"zombieKills") 
local deaths = getAccountData(account,"deaths") 
if deaths == 0 or deaths == "0" then 
ratio = "" 
triggerClientEvent (cliente,"updateClientRatio",cliente,ratio) 
else 
local ratio = round(zombiekills / deaths, rat) 
triggerClientEvent (cliente,"updateClientRatio",cliente,ratio) 
    end 
end 
addEvent("getInfoFromClient", true) 
addEventHandler("getInfoFromClient", getRootElement(), ratio) 

Link to comment
Your problem is that you check if deaths = 0 or "0" when it == nil, so it runs the else and since deaths is nil, it doesnt divide by zombiekills and thus zombiekills becomes your ratio.

Change line 12 in your code quote to:

if deaths == 0 or deaths == "0" or deaths == nil then

it does same problem, btw my deaths data its 1 and kills 960. this is the current code,

function ratio(cliente) 
local account = getPlayerAccount (cliente) 
local zombiekills = getAccountData(account,"zombieKills") 
local deaths = getAccountData(account,"deaths") 
if deaths == 0 or deaths == "0" or deaths == nil then 
ratio = "" 
triggerClientEvent (cliente,"updateClientRatio",cliente,ratio) 
else 
local ratio = round(zombiekills / deaths, rat) 
triggerClientEvent (cliente,"updateClientRatio",cliente,ratio) 
    end 
end 
addEvent("getInfoFromClient", true) 
addEventHandler("getInfoFromClient", getRootElement(), ratio) 

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...