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

Datastores are not working properly?

Asked by 6 years ago
Edited 6 years ago

Hi,

I've recently started using DataStoreService for the first time, and wanted to implement them into my game to store users' Credits. It doesn't seem to error anywhere else, but it fails to get the player's data when they join the game.

Here's my code:

local DSS = game:GetService("DataStoreService")
local playerCreditScore = DSS:GetDataStore("playerCreditScore")

game.Players.PlayerAdded:Connect(function(Player)
    local leaderstats = Instance.new("Model")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = Player
    local Credits = Instance.new("IntValue")
    Credits.Name = "Credits"
    Credits.Parent = leaderstats

    local function getCredit()
        return playerCreditScore:GetAsync(Player.UserID)
    end

    local success, credit = pcall(getCredit)

    if success then
        Credits.Value = credit
    else
        warn("Could not get ", Player.Name,"'s credits.")
    end

end)

game.Players.PlayerRemoving:Connect(function(Player)
    local success = pcall(function()
        playerCreditScore:SetAsync(Player.UserId, Player.leaderstats.Credits.Value)
    end)
    if success then
        print("Successfully saved ", Player.Name,"'s credits.")
    else
        warn("Could not save ", Player.Name,"'s credits.")
    end
end)

FYI, the game has Filtering Enabled enabled.

Thanks

0
Why did you do :IncrementAsync(). Does that work if the values were never there in the first place? How's it going to increment numbers that were never SetAsynced before-hand? Just wondering because I never use :IncrementAsync() and haven't seen it use in a while or ever I guess. KingLoneCat 2642 — 6y
0
hmm, I just figured it was relevant because money is numerical. I'll try SetAsync. Trainsparency 39 — 6y
0
--it didn't change anything. Trainsparency 39 — 6y
0
In line 13. UserId, not UserID User#19524 175 — 6y
View all comments (2 more)
0
You should have made that an answer, lol Trainsparency 39 — 6y
0
--or not, it didnt work. Trainsparency 39 — 6y

Answer this question