Jump to content

index problem


Blinker.

Recommended Posts

Hello ,

this is a piece of my code where the problem occurs

  
if (#getAlivePlayers() == 1) then 
for i ,v in ipairs (getAlivePlayers()) do 
score[v] = score[v] + 4 
end 
end 
  
  

so when a player wins the round , i get:

Error: attempt to perform arithmetic on field '?' (a nil value)

Thanks.

Link to comment

score[v] doesn't have a value yet, that's why that happens. The common solution is to do score[v] = (score[v] or 0) + 4.

Since you are checking if the table only contains one value, you might as well do:

local alivePlayers = getAlivePlayers() 
if(#alivePlayers == 1)then 
    local player = alivePlayers[1] 
    score[player] = (score[player] or 0) + 4 
end 

Link to comment
it's working thanks,

i have another question when i use

  
example[player][1] = Smth 
  

if gives me the same error so i wonder what's the problem , btw it's another script not the same one .. just giving an example

Thanks again

You didn't define example[player] as a table

example[player] = { } 
example[player][1] = value 

Link to comment

Yes it does well what i'm trying to do is to save the player's nicks .. that's what i'm trying to do :

function onPlayerChangeNick(o,n) 
if  not Names then 
Names = {} 
Names[getPlayerSerial(source)] = o , n 
  
else 
Names[getPlayerSerial(source)] = Names[getPlayerSerial(source)] , o ,n 
end 
for i , v in ipairs (Names) do 
outputChatBox(Names[getPlayerSerial(source)]) 
end 
end 
addEventHandler('onPlayerChangeNick',root,onPlayerChangeNick) 
  

Link to comment
local Names = {} 
function onPlayerChangeNick(o,n) 
local pSerial = getPlayerSerial(source) 
if not Names[pSerial] then 
       Names[pSerial] = {} 
end 
table.insert(Names[pSerial], {o, n}) 
for i , v in ipairs (Names[pSerial]) do 
outputChatBox(i..": Old: "..v[1].." -> New: "..v[2], source) 
end 
end 
addEventHandler('onPlayerChangeNick',root,onPlayerChangeNick) 

Link to comment

Inside the changeNick function:

If the player hasn't been registered in the 'Names' table, then create a new position (Names[source]) and place a table inside that registry (Names[source] = {}).

Then insert the nick.

If the player already has been registered in the 'Names' table then loop the table of his nicks (for _, nick in ipairs(Names[source]) and check the value of variable 'nick' against the variable of the new nick that player changed to. (In this case you use 'n' as the variable of it).

If the nick is found then use the 'return' statement inside the for loop to quit it (and the function too) so that it doesn't save the nick.

By the way: I remember that this function has some special treatment;

Example: If you have an antispam nickname script that doesn't let you change your nick more than 3 times in 5 secs for example, at the third time you're gonna change it, even if the antispam nick script 'blocks' you from doing it, the event onPlayerChangeNick is triggered, so you'd have to double check if the nick did change or not (to prevent your names-script storing unreal values)

Link to comment

Hello , i've tried what LinKin said to stop repeating the same names ..

this is what i tried:

  
local Names = {} 
function onPlayerChangeNick(o,n) 
local pSerial = getPlayerSerial(source) 
if not Names[pSerial] then 
       Names[pSerial] = {} 
       table.insert(Names[pSerial], {o, n}) 
       else 
       for i , nick in ipairs (Names[pSerial]) do 
       if (not nick == o) or (not (nick == n)) then 
           table.insert(Names[pSerial], {o, n}) 
end 
end 
end 
for i , v in ipairs (Names[pSerial]) do 
outputChatBox(i..": Old: "..v[1].." -> New: "..v[2], source) 
end 
end 
addEventHandler('onPlayerChangeNick',root,onPlayerChangeNick) 
  
--ERROR : aborting; infinite running script 'script name' 
  

Thanks.

Link to comment
  
local Names = {} 
function onPlayerChangeNick(o,n) 
    local pSerial = getPlayerSerial(source) 
    if not Names[pSerial] then 
       Names[pSerial] = {} 
       table.insert(Names[pSerial], {o, n}) 
    else 
        for i , nick in ipairs(Names[pSerial]) do 
            if nick ~= o and nick ~= n then 
                table.insert(Names[pSerial], {o, n}) 
            end 
        end 
    end 
    for i , v in ipairs (Names[pSerial]) do 
        outputChatBox(i..": Old: "..v[1].." -> New: "..v[2], source) 
    end 
end 
addEventHandler('onPlayerChangeNick',root,onPlayerChangeNick) 
  
--ERROR : aborting; infinite running script 'script name' 
  
  

Link to comment
  
local Names = {} 
function onPlayerChangeNick(o,n) 
    local pSerial = getPlayerSerial(source) 
    if not Names[pSerial] then 
       Names[pSerial] = {} 
       table.insert(Names[pSerial], {o, n}) 
    else 
        table.insert(Names[pSerial], {o, n}) 
    end 
    for i , v in ipairs (Names[pSerial]) do 
        outputChatBox(i..": Old: "..v[1].." -> New: "..v[2], source) 
    end 
end 
addEventHandler('onPlayerChangeNick',root,onPlayerChangeNick) 
  
--ERROR : aborting; infinite running script 'script name' 
  
  

Link to comment

Well really, just take this working code....

Read what I've commented inside of it tho.

local namesTable = { } 
  
addEventHandler("onPlayerChangeNick", root, 
function(oldNick, newNick) 
    local pSerial = getPlayerSerial(source) 
    if not namesTable[pSerial] then namesTable[pSerial] = { } end -- Add new entry if doesn't exist. 
     
    for _, aNick in ipairs(namesTable[pSerial]) do 
        if aNick == newNick then return end -- Nickname already found in the entry; Quit the function and don't save it. 
    end 
     
    table.insert(namesTable[pSerial], newNick) 
end) 
  
  
  
-- Now to show you how to collect all the data from the namesTable: 
  
addCommandHandler("checkdata", 
function(thePlayer) 
    for aSerial, nicksTable in pairs(namesTable) do -- Loop the "mother table" must be in pairs as it is not an indexed table. 
     
        outputChatBox("Nicknames on Serial: "..aSerial) 
         
        for _, aNick in ipairs(nicksTable) do -- Loop the table inside each entry of the "mother table"; Since it's an indexed table, you can use in ipairs. 
            outputChatBox(_..". "..aNick) 
        end 
         
    end 
end) 

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