Skip to content

Commit

Permalink
feat(server/player): support getting/setting nested metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
mafewtm authored and ChatDisabled committed Feb 10, 2025
1 parent ebfbd3e commit 8749995
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions server/player.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1124,9 +1124,23 @@ function SetMetadata(identifier, metadata, value)

if not player then return end

local oldValue = player.PlayerData.metadata[metadata]
local oldValue

player.PlayerData.metadata[metadata] = value
if metadata:match('%.') then
local metaTable, metaKey = metadata:match('([^%.]+)%.(.+)')

if metaKey:match('%.') then
lib.print.error('cannot get nested metadata more than 1 level deep')
end

oldValue = player.PlayerData.metadata[metaTable][metaKey]

player.PlayerData.metadata[metaTable][metaKey] = value
else
oldValue = player.PlayerData.metadata[metadata]

player.PlayerData.metadata[metadata] = value
end

UpdatePlayerData(identifier)

Expand Down Expand Up @@ -1170,7 +1184,17 @@ function GetMetadata(identifier, metadata)

if not player then return end

return player.PlayerData.metadata[metadata]
if metadata:match('%.') then
local metaTable, metaKey = metadata:match('([^%.]+)%.(.+)')

if metaKey:match('%.') then
lib.print.error('cannot get nested metadata more than 1 level deep')
end

return player.PlayerData.metadata[metaTable][metaKey]
else
return player.PlayerData.metadata[metadata]
end
end

exports('GetMetadata', GetMetadata)
Expand Down

0 comments on commit 8749995

Please sign in to comment.