Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Safe player pointers
- -- Returns nil if player is not available
- local players = {}
- local function OnLogin(event, player)
- local guid = player:GetGUIDLow()
- if (players[guid] == false) then -- use a small trick to use false as true here, while using it as false in SafeGetPlayer
- players[guid] = player -- renew
- end
- end
- local function OnLogout(event, player)
- players[player:GetGUIDLow()] = false -- false marks that the player should be saved back to store on login
- end
- RegisterPlayerEvent(3, OnLogin)
- RegisterPlayerEvent(4, OnLogout)
- -- Functions for scripts:
- -- Store player (from now on)
- function Player:Store() -- Can be used as player:Store()
- players[self:GetGUIDLow()] = self
- end
- -- Remove player from store
- function Player:Remove(guidlow) -- Can be used as player:Remove() or player.Remove(guidlow) or Player.Remove(guidlow) or Player:Remove(guidlow)
- players[guidlow or self:GetGUIDLow()] = nil
- end
- -- Get player or nil
- function SafeGetPlayer(guidlow) -- Can be used as SafeGetPlayer(guidlow)
- return players[guidlow] or nil
- end
- -- example:
- local function OnChat(event, player, msg, lang, typ, misc)
- if (msg == "store") then
- player:Store()
- end
- if (msg == "remove") then
- player:Remove()
- end
- if (msg == "test") then
- local guid = player:GetGUIDLow()
- CreateLuaEvent(function() print(guid, SafeGetPlayer(guid)) end, 500, 0)
- end
- end
- RegisterPlayerEvent(18, OnChat)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement