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

Script isn't saving player data to data store, have i referenced it correctly? [closed]

Asked by 4 years ago
Edited 4 years ago

hi can anyone help figure out why this isnt saving the data?

-- Data Storage
local DataStoreService = game:GetService("DataStoreService")

local playerData = DataStoreService:GetDataStore("PlayerData")



local function onPlayerJoin(player)  -- Runs when players join

    local leaderstats = Instance.new("Folder")  --Sets up leaderstats folder

    leaderstats.Name = "leaderstats"

    leaderstats.Parent = player



    local bucks = Instance.new("IntValue") --Sets up value for leaderstats

    bucks.Name = "Bucks"

    bucks.Parent = leaderstats



    local exp = Instance.new("IntValue") --Sets up value for leaderstats

    exp.Name = "Experience"

    exp.Parent = leaderstats


    local rank = Instance.new("StringValue") --Sets up value for leaderstats

    rank.Name = "Rank"

    rank.Parent = leaderstats



    local playerUserId = "Player_" .. player.UserId  --Gets player ID

    local data = playerData:GetAsync(playerUserId)  --Checks if player has stored data

    if data then

        bucks.Value = data['Bucks']

        exp.Value = data['Experience']

        rank.Value = data['Rank']

    else

        -- Data store is working, but no current data for this player

        bucks.Value = 50

        exp.Value = 50

        rank.Value = "Rec"

    end

end



local function create_table(player)

    local player_stats = {}

    for _, stat in pairs(player.leaderstats:GetChildren()) do

        player_stats[stat.Name] = stat.Value

    end

    return player_stats

end



local function onPlayerExit(player)  --Runs when players exit



    local player_stats = create_table(player)

    local success, err = pcall(function()

        local playerUserId = "Player_" .. player.UserId

        playerData:SetAsync(playerUserId, player_stats) --Saves player data

        print("Player Data Stored")

    end)



    if not success then

        warn('Could not save data!')

    end

end



game.Players.PlayerAdded:Connect(onPlayerJoin)

game.Players.PlayerRemoving:Connect(onPlayerExit)

Closed as Non-Descriptive by JesseSong

This question has been closed because its title or content does not adequately describe the problem you are trying to solve.
Please ensure that your question pertains to your actual problem, rather than your attempted solution. That is, you were trying to solve problem X, and you thought solution Y would work, but instead of asking about X when you ran into trouble, you asked about Y.

Why was this question closed?