Whenever the users collects a gorilla it’s supposed to update the gorilla leaderstat value and the save the value when I first joined and collected one it updated my leaderstat value however when I rejoined the game my leaderstat value was reset back to 0, does anyone know what the issue is? (New in datastoreservice, and I have api services enabled)
game.Players.PlayerAdded:Connect(function(player)
local stat = Instance.new("Folder")
stat.Name = "leaderstats"
stat.Parent = player
local gorilla = Instance.new("IntValue")
gorilla.Name = "Gorillas"
gorilla.Value = 0
gorilla.Parent = stat
local data
local success, errormessage = pcall(function()
data = game:GetService("DataStoreService"):GetDataStore("GorillaData"):GetAsync("gorillas-"..player.UserId)
end)
if success then
gorilla.Value = data
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
game:GetService("DataStoreService"):GetDataStore("GorillaData"):SetAsync("gorillas-"..player.UserId, player.leaderstats.Gorillas.Value)
end)
if not success then
warn("Failed to save gorilla count for player "..player.Name..": "..errormessage)
end
end)
game.ReplicatedStorage.Badge.OnServerEvent:Connect(function(plr, badge)
local Badgeservice = game:GetService("BadgeService")
local gorillas = plr:WaitForChild("leaderstats"):WaitForChild("Gorillas")
if not Badgeservice:UserHasBadgeAsync(plr.UserId, badge) and badge ~= 0 then
Badgeservice:AwardBadge(plr.UserId, badge)
gorillas.Value += 1
end
end)
So like this? And yes I have tested it both in studio and in actual server
local Storage = game:GetService("DataStoreService"):GetDataStore("GorillaData")
game.Players.PlayerAdded:Connect(function(player)
local stat = Instance.new("Folder")
stat.Name = "leaderstats"
stat.Parent = player
local gorilla = Instance.new("IntValue")
gorilla.Name = "Gorillas"
gorilla.Value = 0
gorilla.Parent = stat
local data
local success, errormessage = pcall(function()
data = Storage:GetAsync("GorillaData" .. player.UserId)
end)
if success then
gorilla.Value = data
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
Storage:GetAsync("GorillaData" .. player.UserId)
end)
if not success then
warn("Failed to save gorilla count for player " .. player.Name .. ": "..errormessage)
end
end)
I dont think there’s a need to print the final gorilla number when you “save” because even if i printed it the leaderstat will get reseted back to zero when i rejoin the actual server