I am trying to make it so that every time a player joins an empty table is added to a Dictionary with the player’s name as the index (this works), but for some reason when I try to access the Dictionary later all the values in the table are gone. Can someone explain to me why this is happening (I am going crazy pls help).
ServerScript:
local DatastoreService = game:GetService("DataStoreService")
local DataStore = DatastoreService:GetDataStore("PlayerData")
local CaseHandler = require(script.PlayerCaseHandler)
local ID = 0
game.Players.PlayerAdded:Connect(function(Player)
-- Important
CaseHandler.Cases[Player.Name] = {}
local CasesStore = DatastoreService:GetDataStore(Player.Name.."Cases")
local PlrArrestInfo = Instance.new("Folder")
PlrArrestInfo.Name = "PlrArrestInfo"
PlrArrestInfo.Parent = Player
local PlayerWarrant = Instance.new("StringValue")
PlayerWarrant.Name = "Warrant"
PlayerWarrant.Parent = PlrArrestInfo
local PlayerPriors = Instance.new("IntValue")
PlayerPriors.Name = "Priors"
PlayerPriors.Parent = PlrArrestInfo
local ToolsNotInUse = Instance.new("Folder")
ToolsNotInUse.Parent = Player
ToolsNotInUse.Name = "ToolsNotInUse"
--Important
warn(CaseHandler.Cases)
warn(CaseHandler.IDs)
local Warrant
local Priors
local success, errormessage = pcall(function()
Warrant = DataStore:GetAsync(Player.UserId.."-Warrant")
Priors = DataStore:GetAsync(Player.UserId.."-Priors")
CaseHandler.Cases[Player.Name] = CasesStore:GetAsync(Player.UserId.."-Cases")
end)
if success then
if Priors ~= nil then
Player.PlrArrestInfo.Priors.Value = Priors
else
Player.PlrArrestInfo.Priors.Value = 0
end
if Warrant ~= nil then
Player.PlrArrestInfo.Warrant.Value = Warrant
else
Player.PlrArrestInfo.Warrant.Value = "None."
end
else
warn(errormessage)
end
end)
--Important
game.Players.PlayerRemoving:Connect(function(Player)
local CasesStore = DatastoreService:GetDataStore("PlayerCases")
local success, errormessage = pcall(function()
DataStore:SetAsync(Player.UserId.."-Warrant",Player.PlrArrestInfo.Warrant.Value)
DataStore:SetAsync(Player.UserId.."-Priors",Player.PlrArrestInfo.Priors.Value)
CasesStore:SetAsync(Player.UserId.."-Cases",CaseHandler.Cases[Player.Name])
end)
if success then
print("DataSaved")
else
print("DataNotSaved!")
warn(errormessage)
end
CaseHandler.Cases[Player.Name] = nil
warn(require(script.PlayerCaseHandler).Cases)
end)
--Important
game:GetService("ServerStorage"):FindFirstChild("JailData").Event:Connect(function(Inmate,Charges,Time)
local Table_to_insert = {
["Time"] = Time
}
for i, v in pairs(Charges) do
table.insert(Table_to_insert,v)
end
local TimeTag = os.date("%c")
print(CaseHandler.Cases)
end)
Output:
