Blinker. Posted November 23, 2014 Posted November 23, 2014 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.
arezu Posted November 23, 2014 Posted November 23, 2014 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
Blinker. Posted November 23, 2014 Author Posted November 23, 2014 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
ChaosMTA Posted November 23, 2014 Posted November 23, 2014 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
Blinker. Posted November 24, 2014 Author Posted November 24, 2014 Of Course i did , it was just an example.
Sasu Posted November 24, 2014 Posted November 24, 2014 Does "Smth" have a value? Or you just want to insert "Smth" as a text?
Blinker. Posted November 24, 2014 Author Posted November 24, 2014 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)
Sasu Posted November 25, 2014 Posted November 25, 2014 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)
Blinker. Posted November 25, 2014 Author Posted November 25, 2014 Thanks a lot , last question how to check if the table has the same name ?, like "Blinker" and "Blinker" remove 1 of them i dont want to insert 2 same nicks
Sasu Posted November 25, 2014 Posted November 25, 2014 If I remember correctly, MTA doesnt let you to change to the same nick.
Blinker. Posted November 25, 2014 Author Posted November 25, 2014 I don't mean changing to the same nick , i mean like if the player had "Blinker" name and then he changed to "Sasu" , then he changed back to "Blinker", in the table i will have Blinker,Sasu,Sasu,Blinker .. how to prevent that ? Thanks
'LinKin Posted November 25, 2014 Posted November 25, 2014 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)
MTA Team qaisjp Posted November 25, 2014 MTA Team Posted November 25, 2014 Don't forget how to indent!
Blinker. Posted November 27, 2014 Author Posted November 27, 2014 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.
Anubhav Posted November 27, 2014 Posted November 27, 2014 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'
Anubhav Posted November 28, 2014 Posted November 28, 2014 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'
Blinker. Posted November 28, 2014 Author Posted November 28, 2014 And what's that for ? if i use this it won't delete the repeated names like 'Blinker changed to Blinker1' and then 'Blinker1 changed to Blinker' i want it to save 'Blinker Blinker1' instead of 'Blinker Blinker1 Blinker1 Blinker'
'LinKin Posted November 28, 2014 Posted November 28, 2014 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)
Blinker. Posted November 28, 2014 Author Posted November 28, 2014 I tried that , it saves everything .. even if the names are the same.
Blinker. Posted November 28, 2014 Author Posted November 28, 2014 Fixed , Many Thanks to LinKin helped me at Skype and Thank you all.
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