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

Why isn't my Data Store system working?

Asked by 6 years ago
local dataStore = game:GetService("DataStoreService"):GetDataStore("myStore")

function userEntered(newPlayer)
    local clientFolder = Instance.new("Folder")
    clientFolder.Parent = newPlayer
    clientFolder.Name = "Client Data"

    local knockouts = Instance.new("IntValue")
    knockouts.Parent = clientFolder
    knockouts.Name = "Knockouts"
    knockouts.Value = 0

    local gamesPlayed = Instance.new("IntValue")
    gamesPlayed.Parent = clientFolder
    gamesPlayed.Name = "Games Played"

    local userKey = "usr-"..newPlayer.UserId
    local dataTable = dataStore:GetAsync(userKey)

    if dataTable then
        knockouts.Value = dataTable[1]
        gamesPlayed.Value = dataTable[2]
    else
        print(1)
        local dataTable = {knockouts.Value, gamesPlayed.Value}
        dataStore:SetAsync(userKey, dataTable)
    end
end


function userLeaving(oldPlayer)
    local userKey = "usr-"..oldPlayer.UserId
    local clientFolder = oldPlayer:FindFirstChild("Client Data")
    local dataTable = {clientFolder.Knockouts.Value, clientFolder["Games Played"].Value}

    dataStore:SetAsync(userKey, dataTable)
end

game.Players.PlayerAdded:Connect(userEntered)
game.Players.PlayerRemoving:Connect(userLeaving)

When I run the game, the values show as "0" as if the Data Store did not load the values. Does anyone know why?

Thank you.

0
I recommend WaitForChild instead of FindFirstChild, thats all I can say saSlol2436 716 — 6y

Answer this question