Jump to content

Problem with Arrays


Manuel1948

Recommended Posts

Hey People!

I wanted to make an ID system for my script, and everytime a player joines he should get the less id, which is possible.

On top at the script I define idtable as an array and above i start writing 0 in the first 200 fields.

  
idtable = {} 
  
for x=1,200,1 do 
    idtable[x]=0 
end 
  
  

And in the PlayerJoinHandler, I have the giveplayerID(player) function, but the problem is, that the function didn't write the id in the idtable, the whole table stays at 0. Why?

  
function givePlayerID(player) 
    local id = 1 
    local count = 0 
    local check=false 
     
    for i,v in ipairs(idtable) do 
        if v==0 and check==false then 
            id=i 
            check=true 
        end 
    end  
     
    idtable[id]=id -- Problem Row 
    setElementData(player,"player.id",id) 
end 
  

Link to comment

You want to set a ID like this:

First player: ID 1

Second player: ID 2

Etc, etc? if so, this should work:

idtable = {} 
  
function givePlayerID(player) 
    local id = #idtable +1 
    table.insert(idtable, id) 
    setElementData(player,"player.id",id) 
end 

Link to comment

Well, you can remove the ID when the player leaves, right?

addEventHandler("onPlayerQuit",root, 
function () 
     local id = getElementData(source, "player.id") 
     if (tonumber(id)) then 
          if (idtable[id]) then table.remove(idtable, id) end 
     end 
end) 
  

Link to comment

alright but look at your script for givePlayerID:

  
function givePlayerID(player) 
    local id = #idtable +1 -- this row adds +1 to the size of idtable better to the used fields its not right 
    table.insert(idtable, id) 
    setElementData(player,"player.id",id) 
end 
  
  

Link to comment
alright but look at your script for givePlayerID:
  
function givePlayerID(player) 
    local id = #idtable +1 -- this row adds +1 to the size of idtable better to the used fields its not right 
    table.insert(idtable, id) 
    setElementData(player,"player.id",id) 
end 
  
  

I would say Castillo's script looks a lot more professional. and am not sure on your script.

Link to comment

ok an this is the mistake look:

1. Player gets ID1 | #idtable=1

2. Player gets ID2 | #idtable=2

3. Player gets ID3 | #idtable=3

--And now e.g 2.Player left the server and #idtable=2

--and the next joining player gets ID3 too because the IDs are not shifting

understand?

Edited by Guest
Link to comment
Try to explain more what your code is meant to do like break it down exactly what should be expected from it during each part or something. The way you wrote it almost looks like you've done C++ or something

C++ is like Lua? It's -nothing- like Lua. More alike PHP.

It's night and going to sleep, if you could try to work on this.

function givePlayerID() 
     local idtable = { } 
     setElementData(source, "player.id", idtable[#idtable + 1]) 
end 
addEventHandler("onPlayerJoin", getRootElement(), givePlayerID) 
  
function takePlayerID() 
     removeElementData(source, "player.id") 
end 
addEventHandler("onPlayerQuit", getRootElement(), takePlayerID) 

Edited by Guest
Link to comment
Try to explain more what your code is meant to do like break it down exactly what should be expected from it during each part or something. The way you wrote it almost looks like you've done C++ or something

C++ is like Lua? It's -nothing- like Lua. More alike PHP.

Edited by Guest
Link to comment

#idtable is not a function or something idtable is an self defined array and #idtable returns the count of elements in array

I have an array:

idtable[1]

idtable[2]

idtable[3]

and so on, and the value of every array is default 0, if a player joins, the array should take the value of it number like:

idtable[1]=1

idtable[2]=2

but with this script, it doesn't work:

  
for i,v in ipairs(idtable) do 
        if v==0 and check==false then 
            id=i 
            check=true 
        end 
    end  
    
    idtable[id]=id -- Problem Row 
  

the problem row is the code which isn't executed, maybe the syntax is false?

Link to comment

I'd this:

idtable = {} 
  
for x=1, 200 do 
    idtable[x]=0 
end 
  
function givePlayerID(player) 
    local id = 1 
    local check = false 
    
    for i, v in ipairs(idtable) do 
        if ( v==0 and check==false ) then 
            id=i 
            check=true 
        end 
    end 
  
    idtable[id]=id 
    outputChatBox(tostring(id) ..": ".. tostring(idtable[id])) 
    setElementData(player,"player.id",id) 
    for i, v in ipairs(idtable) do 
        outputChatBox(tostring(v)) 
    end 
end 
addCommandHandler("id",givePlayerID) 

And it returns like this: 1: 1, it doesn't return 0.

Link to comment

Thx, the mistake was in the loop " if v==0 and check==false then" is false, "if ( v==0 and check==false ) then" is true, but what does lua do without the ()

Code is done from left to right, does it eman lua reads "if v==0 and check==false then" as "if v== (0 and check==false) then" which is always false?

Link to comment

My function( tested ) and more better :)

local id = { } 
  
function givePlayerID( player ) 
    if getElementData( player,"player.id" ) then  
        return 
    end 
    if not id[ player ] then  
        id[ player ] = 1  
    end 
     for _,v in pairs( getElementsByType( "player" ) ) do 
        if getElementData( v,"player.id" ) == id[ player ] then  
            id[ player ] = id[ player ] + 1  
            givePlayerID( player ) 
            return 
        end 
    end 
    setElementData( player,"player.id",id[ player ] )  
    id[ player ] = nil  
end 
addCommandHandler("id",givePlayerID) 

EDIT:Code modified.

Link to comment
But is working or not? because that wouldn't make any effect.

Dont you read my answers or dont you understand them?

Thx, the mistake was in the loop " if v==0 and check==false then" is false, "if ( v==0 and check==false ) then" is true, but what does lua do without the ()

Code is done from left to right, does it eman lua reads "if v==0 and check==false then" as "if v== (0 and check==false) then" which is always false?

This solved my problem, and it were only the brackets, maybe this is lua default way to process: lua reads "if v==0 and check==false then" as "if v== (0 and check==false) then"

@kenix: its working right now, but if I have problems again, I will read through your solution, because now i don't want to.

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