When I save my data in game and use GetAsync it gets the data back just fine, but if i rejoin and use GetAsync the data is back to 0.
for _,v in pairs(game.Players:GetChildren())do --saving god power local success, err = pcall(function() --print(v.PlayerInfo.Stats.Followers.Value) return godPower:SetAsync(v.UserId, v.leaderstats:FindFirstChild("God Power").Value) end) if success then print("godpower saved") end local success, err = pcall(function()--god power currentGP = godPower:GetAsync(v.UserId) or 0 end) print("SAVED: "..currentGP) end
this prints "SAVED: 146"
Players.PlayerAdded:Connect(function(player) local success, err = pcall(function()--god power currentGP = godPower:GetAsync(player.UserId) or 0 end) if success then print("power:"..currentGP) player.leaderstats:FindFirstChild("God Power").Value = currentGP end end)
this prints "power:0"
Ok so I found the problem
local function onCharacterRemoving(character) -- Get the player and their HumanoidRootPart and save their death location local player = Players:GetPlayerFromCharacter(character) local hrp = character:FindFirstChild("HumanoidRootPart") local player = Players:GetPlayerFromCharacter(character) goddied(player) end
in the goddied function it sets the godpower to 0 again, but this happens when the player joins because
local function onPlayerAdded(player) player.CharacterAdded:Connect(onCharacterAdded) player.CharacterRemoving:Connect(onCharacterRemoving) --player info local playerInfo = game.ServerStorage.PlayerInfo:Clone() playerInfo.Parent = player end
onCharacterRemoving is fired once when the player joins so to avoid this problem, we just need to not have onCharacterRemoving run the first time the player joins. Hope this helps if anyone else ever runs into the same issue.