#datastore/leaderstates scripts has a problem

1 messages · Page 1 of 1 (latest)

woven breach
#

leaderstats code

local playerDataStore = DataStoreService:GetDataStore("PlayerData")

game.Players.PlayerAdded:Connect(function(Player)

    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = Player

    local wins = Instance.new("IntValue")
    wins.Name = "Wins"
    wins.Value = 0
    wins.Parent = leaderstats

    local coins = Instance.new("IntValue")
    coins.Name = "Coins"
    coins.Value = 0
    coins.Parent = leaderstats


    local successwins, savedwins = pcall(function()
        return playerDataStore:GetAsync(tostring(Player.UserId) .. "_wins")  
    end)

    local successcoins, savedcoins = pcall(function()
        return playerDataStore:GetAsync(tostring(Player.UserId) .. "_coins")  
    end)

    if successwins and savedwins then
        wins.Value = savedwins
    else
        warn("Failed to load wins data for player " .. Player.Name)
    end

    if successcoins and savedcoins then
        coins.Value = savedcoins
    else
        warn("Failed to load coins data for player " .. Player.Name)
    end
end)

game.Players.PlayerRemoving:Connect(function(Player)

    local wins = Player.leaderstats and Player.leaderstats:FindFirstChild("wins")
    local coins = Player.leaderstats and Player.leaderstats:FindFirstChild("coins")

    if wins and coins then
        local success, errorMessage = pcall(function()
            playerDataStore:SetAsync(tostring(Player.UserId) .. "_wins", wins.Value)
            playerDataStore:SetAsync(tostring(Player.UserId) .. "_coins", coins.Value)
            
        end)

        if success then
            print("Successfully saved data for player " .. Player.Name)
        else
            warn("Failed to save data for player " .. Player.Name .. ": " .. errorMessage)
        end
    end
end)```
winged galleon
#

Also in player removing functions you miscall the coins and wins

#

That should be "Wins" and "Coins"

#

Also idk if it's my phone or else but why is there some code duplicated?