Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

My datastore isn't saving right and I don't know what's wrong?

Asked by 3 years ago

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"

0
Try to remove the get async in the first script. Maybe that helps. Soban06 410 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

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.

0
ok my bad it's not that its fired once when the player joins but rather when the player leaves it also removes the char not just when they die jamespringles 105 — 3y
0
ok my bad it's not that its fired once when the player joins but rather when the player leaves it also removes the char not just when they die jamespringles 105 — 3y
Ad

Answer this question