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

Why Does My Data Store Come Out With No Error Messages?

Asked by 3 years ago

I am trying to expand my coding knowledge by (attempting) to scripting a commonly used scripting feature, data stores. I have searched up data stores on the roblox dev hub and still come out with an un-working, no error message script. Help would be greatly appreciated.

Code:

local DataStoreService = game:GetService('DataStoreService')
local GoldCurrency = DataStoreService:GetDataStore('PlayersGold')

game.Players.PlayerAdded:Connect(function(player)
    local PlayerID = player.UserId
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = 'leaderstats'
    leaderstats.Parent = player

    local Gold = Instance.new("IntValue")
    Gold.Name = 'Gold'
    Gold.Parent = leaderstats

    local success, currentExperience = pcall(function()
        return GoldCurrency:GetAsync(PlayerID)
    end)

    if success then
        Gold.Value = GoldCurrency
    else
        Gold.Value = 0
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    local PlayerID = player.UserId

    local success, err = pcall(function()
        GoldCurrency:SetAsync(PlayerID, player.leaderstats.Gold.Value)
    end)

    if success then
        print("It Worked!")
    else
        print(err)
    end
end)

2 answers

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

Datastores do not work in Studio. Try in the actual game client

0
Yes I have, It still doesn't save the data wolftamer894 50 — 3y
0
actually it does work in studio NGC4637 602 — 3y
Ad
Log in to vote
0
Answered by
NGC4637 602 Moderation Voter
3 years ago

I've encountered this problem before, and fixed it by realizing i have to enable API Services (in game settings) first, otherwise it will not work.

0
also the api reference states: DataStoreService cannot be used in Studio if a game is not configured to allow access to API services. See the Data Stores article for instructions. NGC4637 602 — 3y
0
Article: https://developer.roblox.com/en-us/articles/Data-store (it at the bottom of the article btw) NGC4637 602 — 3y
0
I already have that enabled wolftamer894 50 — 3y

Answer this question