Hello,
I need help from someone who knows how to use the Data module in AGF. Right now I’m building an admin command system for my game, and for some inexplicable reason, when I change table data with the command, it changes for other players in the server as well.
Here's what runs when the command is executed
local sets = RDS:Get(player, "Sets")
if table.find(sets, set) then warn("CMD Fail: player \""..player.Name.."\" already has set \""..set.."\"") return end
table.insert(sets, set)
RDS:Save(player, "Sets", sets)
print("CMD: "..player.Name.." given Set "..set)
A few things here. RDS
is the name for the service that handles my data and it has functions that build off the basic functionality the Data module has (RDS
code posted below). "Sets"
is the key of the value I’m trying to change; the value is a table of strings. set
is a string value that is trying to be added to the table.
Another thing; when initially tried saving the new value, RDS
told me that new value was the same as the old value already in the cache. Why is this happening if I haven’t even saved the value yet? I ended up added an “override” parameter to the function in RDS
to ignore this.
The end result causes the value for the key Sets
to change for all the players in the server. I’ve spent over two hours pulling out my hair on this. Any help would be appreciated. Thanks!
RDS get code
--ignore the keygroup stuff
function RealDataService:Get(player, key, keyGroup)
print("Data got:", player, key, keyGroup)
if keyGroup == "LevelData" then
return {Level = Get(player, "Level"), StatPoints = Get(player, "StatPoints"), Strength = Get(player, "Strength"), Agility = Get(player, "Agility"), Defense = Get(player, "Defense"), CurrentXP = Get(player, "CurrentXP"), Money = Get(player, "Money")}
elseif keyGroup == "AbilityData" then
return {Sets = Get(player, "Sets"), Inventory = Get(player, "Inventory"), Level = Get(player, "Level"), Strength = Get(player, "Strength"), Agility = Get(player, "Agility"), Defense = Get(player, "Defense")}
elseif not keyGroup then
--this is where the function should end up
return Get(player, key)
else
warn("Invalid keyGroup: "..keyGroup)
end
end
function Get(player, key)
local dataFile = Data.ForPlayer(player)
local success, value = dataFile:Get(key):Await()
if success then
return value
else
warn("Could not get " .. key .. " for Player "..player.Name)
end
end
RDS save code
function RealDataService:Save(player, key, newValue, override)
local function Fail()
--made this function to ignore the equality warning
if not override then return end
warn("The above warning was overridden!")
end
if newValue == nil then
warn("Data warning! "..key.." just tried to save as nil for "..player.Name.."! Value not saved.")
Fail()
end
local oldValue = Get(player, key)
if oldValue then
if newValue == oldValue then
-- this is where it was telling me the oldValue = newValue. how could this be possible?
warn("New value of "..(type(newValue) == "table" and "(table)" or newValue).." is equal to "..(type(oldValue) == "table" and "(table)" or oldValue).." for key "..key.." for "..player.Name..". Value not saved.")
Fail()
end
if type(oldValue) == "number" then
print(key..": "..oldValue.." --> "..newValue.." for "..player.Name)
elseif type(oldValue) == "table" then
print(key.." set to a new table for "..player.Name)
end
Set(player, key, newValue)
else
warn("Key ("..key..") does not exist in "..player.Name.."'s file. Value not saved.")
end
end
function Set(player, key, value)
local dataFile = Data.ForPlayer(player)
dataFile:Set(key, value):Await()
RealDataService:FireClientEvent("DataChanged", player)
end