lgeri000 Posted September 27, 2020 Posted September 27, 2020 Hello, can anybody tell me why is this ids table stay empty? local maxPlayers = getMaxPlayers() local ids = {} addEventHandler("onPlayerJoin", root, function() local player = source for i = 1, maxPlayers do if (ids[i] == false) then ids[i] = i setElementData(player,"id",i) break end end end ) addEventHandler("onPlayerQuit", root, function() local i = getElementData(source,"id") ids[i] = false setElementData(source,"id",false) end ) this would be an id-system 1
Moderators IIYAMA Posted September 27, 2020 Moderators Posted September 27, 2020 3 hours ago, lgeri000 said: Hello, can anybody tell me why is this ids table stay empty? local maxPlayers = getMaxPlayers() local ids = {} addEventHandler("onPlayerJoin", root, function() local player = source for i = 1, maxPlayers do if (ids[i] == false) then ids[i] = i setElementData(player,"id",i) break end end end ) addEventHandler("onPlayerQuit", root, function() local i = getElementData(source,"id") ids[i] = false setElementData(source,"id",false) end ) this would be an id-system use nil instead of false. false = is still something, even if it is not considered as positive value. nil = nothing false == nil -- false (unlike in languages like JS, where that is true because the === operator is not used.) Also increment the ID value, instead of doing it based on maxPlayers. Keep it simple. local ID = 0 --onPlayerJoin ID = ID + 1 setElementData(player,"id",ID) -- Do you want to improve your Lua programming skills and make less mistakes? Start with Lua Language Server! Useful functions 3x Spoiler checkPassiveTimer getScreenStartPositionFromBox getPedGender Tutorials 4x Spoiler Scaling DX Events Attach an addEventHandler on a group of elements Debugging
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