Manuel1948 Posted January 4, 2012 Share Posted January 4, 2012 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
Castillo Posted January 4, 2012 Share Posted January 4, 2012 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
Manuel1948 Posted January 4, 2012 Author Share Posted January 4, 2012 i don't want another script code, i rather want to know why my script isn't working, btw. your script is nonesense, because if they are 5 players online and the 3rd for example leaves the server, the next joining player get the id 6 and not 3 Link to comment
Castillo Posted January 4, 2012 Share Posted January 4, 2012 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
Manuel1948 Posted January 4, 2012 Author Share Posted January 4, 2012 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
Castillo Posted January 4, 2012 Share Posted January 4, 2012 I don't get what do you mean, that works just fine. P.S: Your script doesn't really make sense to me. Link to comment
Manuel1948 Posted January 4, 2012 Author Share Posted January 4, 2012 ok the say me what does #idtable stands for Link to comment
TheNightRider Posted January 4, 2012 Share Posted January 4, 2012 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
Castillo Posted January 4, 2012 Share Posted January 4, 2012 It'll count the table items. P.S: It's "tell me" . Link to comment
Manuel1948 Posted January 4, 2012 Author Share Posted January 4, 2012 (edited) 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 January 4, 2012 by Guest Link to comment
Castillo Posted January 4, 2012 Share Posted January 4, 2012 Well, you can re-order the ID's after someone quits. Link to comment
TheNightRider Posted January 4, 2012 Share Posted January 4, 2012 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 Link to comment
Manuel1948 Posted January 4, 2012 Author Share Posted January 4, 2012 I could, but I want SAMP-like IDs and not changing IDs... Why you can't tell me, why my script does not write the id in the id table Link to comment
myonlake Posted January 4, 2012 Share Posted January 4, 2012 (edited) 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 January 4, 2012 by Guest Link to comment
TheNightRider Posted January 4, 2012 Share Posted January 4, 2012 If your referring to me I would tell you but simply ive got no clue myself ive looked up about the #idtable in the mta wikki but couldn't find anything, sorry couldn't help. What I meant like C++ was how your doing your code I know the difference between script coding and program coding. Link to comment
TheNightRider Posted January 4, 2012 Share Posted January 4, 2012 (edited) 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 January 4, 2012 by Guest Link to comment
Manuel1948 Posted January 4, 2012 Author Share Posted January 4, 2012 #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
myonlake Posted January 4, 2012 Share Posted January 4, 2012 Check my reply on prev. page. It returns as +1 all times I guess but try to fix it, gonna sleep naw bai. Checking tomorrow. Link to comment
Castillo Posted January 4, 2012 Share Posted January 4, 2012 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
Manuel1948 Posted January 5, 2012 Author Share Posted January 5, 2012 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
Castillo Posted January 5, 2012 Share Posted January 5, 2012 Nah, as far as I know it's just to keep your script organized . Maybe I'm wrong too . So, the script does work now? I didn't touch anything as far as I know, so if it does now, why it didn't do before? Link to comment
Manuel1948 Posted January 5, 2012 Author Share Posted January 5, 2012 you touched the script with a ( and a ) in the if-Statement in for-loop Link to comment
Castillo Posted January 5, 2012 Share Posted January 5, 2012 But is working or not? because that wouldn't make any effect. Link to comment
Kenix Posted January 5, 2012 Share Posted January 5, 2012 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
Manuel1948 Posted January 7, 2012 Author Share Posted January 7, 2012 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
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