Hello! I’ve decided to use profilservice for the first time to help prevent duping in my game and dataloss and its ease of use.
Im struggling with the fact its setting my cash data as nil. Does anyone know why this is??
local ProfileService = require(script.ProfileService)
local Players = game:GetService("Players")
local ProfileTemplate = {
Cash = 0,
Rebirths = 0
}
local ProfileStore = ProfileService.GetProfileStore(
"Playera",
ProfileTemplate
)
local Profiles = {}
Players.PlayerAdded:Connect(function(Player)
local profile = ProfileStore:LoadProfileAsync("Player_"..Player.UserId)
profile:AddUserId(Player.UserId)
profile:Reconcile()
if profile then
profile:ListenToRelease(function()
Profiles[Player] = nil
Player:Kick("Kicked to prevent dataloss")
end)
if Player:IsDescendantOf(Players) then
Profiles[Player] = profile
else
profile:Release()
end
else
Player:Kick("Error loading profile, Kicked to prevent dataloss")
end
end)
Players.PlayerRemoving:Connect(function(Player)
local profile = Profiles[Player]
if profile then
profile:Release()
end
end)
local DataHandler = {}
function DataHandler:Get(Player)
local profile = Profiles[Player]
if profile then return profile end
end
return DataHandler
local ProfileService = require(script.ProfileService)
local Players = game:GetService("Players")
local ProfileTemplate = {
Cash = 0,
Rebirths = 0
}
local ProfileStore = ProfileService.GetProfileStore(
"Playera",
ProfileTemplate
)
local Profiles = {}
function PlayerJoined(Player)
local profile = ProfileStore:LoadProfileAsync("Player_"..Player.UserId)
profile:AddUserId(Player.UserId)
profile:Reconcile()
if profile then
profile:ListenToRelease(function()
Profiles[Player] = nil
Player:Kick("Kicked to prevent dataloss")
end)
if Player:IsDescendantOf(Players) then
Profiles[Player] = profile
else
profile:Release()
end
else
Player:Kick("Error loading profile, Kicked to prevent dataloss")
end
end
for _, Player in ipairs(Players:GetPlayers()) do
task.spawn(PlayerJoined, Player)
end
Players.PlayerAdded:Connect(PlayerJoined)
Players.PlayerRemoving:Connect(function(Player)
local profile = Profiles[Player]
if profile then
profile:Release()
end
end)
local DataHandler = {}
function DataHandler:Get(Player)
local profile = Profiles[Player]
if profile then return profile end
end
return DataHandler
I still get the same error when trying to add values…
Heres a simple script that im using to test it
local DataHandler = require(game.ReplicatedStorage.DataHandler)
game.Players.PlayerAdded:Connect(function(Player)
while true do
wait(1)
local Data = DataHandler:Get(Player)
if Data then
Data.Cash += 500
print(Data.Cash)
else
print("Data not loaded yet")
end
end
end)