Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(server/player): support getting/setting nested metadata #653

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions server/player.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1114,9 +1114,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 @@ -1160,7 +1174,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
Loading