1+ atomic .bind = atomic .bind or {
2+ --- @type table<number , table<number , fun ( player : Player )>>
3+ _storage = {},
4+ --- @type table<number , number> -- [registrationId] = key
5+ _map = {},
6+ _lastBindId = 0
7+ }
8+
9+ --- @param key number Index of the key
10+ --- @param callback fun ( player : Player )
11+ --- @return number registerationId
12+ function atomic .bind .bind (key , callback )
13+ if (type (atomic .bind ._storage [key ]) ~= " table" ) then
14+ atomic .bind ._storage [key ] = {}
15+ end
16+
17+ atomic .bind ._lastBindId = atomic .bind ._lastBindId + 1
18+ local id = atomic .bind ._lastBindId
19+
20+ atomic .bind ._storage [key ][id ] = callback
21+ atomic .bind ._map [id ] = key
22+
23+ return id
24+ end
25+
26+ --- @param registerationId number
27+ function atomic .bind .unbind (registerationId )
28+ local key = atomic .bind ._map [registerationId ]
29+ if (type (key ) ~= " number" ) then
30+ return
31+ end
32+
33+ local tab = atomic .bind ._storage [key ]
34+
35+ if (type (tab ) ~= " table" ) then
36+ return
37+ end
38+
39+ tab [registerationId ] = nil
40+ atomic .bind ._map [registerationId ] = nil
41+ end
42+
43+ --- @param player Player
44+ --- @param key number
45+ local function keybind_handler (player , key )
46+ if (not IsFirstTimePredicted ()) then
47+ return
48+ end
49+
50+ local tab = atomic .bind ._storage [key ]
51+ if (type (tab ) ~= " table" ) then return end
52+
53+ for _ , callback in pairs (tab ) do
54+ callback (player )
55+ end
56+ end
57+
58+ hook .Add (" PlayerButtonDown" , " atomic.bind" , keybind_handler )
0 commit comments