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

DataStore is not saving my variables?

Asked by
edwisc 1
3 years ago

I'm trying to write a script where every second it saves data =, but it really isn't working :/ Could anyone help?

My script:

local dataStoreService = game:GetService("DataStoreService") local myDataStore = dataStoreService:GetDataStore("myDataStore")

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

--- Leaderstats Variables


--- Hidden Variables

local hiddenVariables = Instance.new("Folder",player)
hiddenVariables.Name = "hiddenVariables"


local eventsCompleted = Instance.new("IntValue",hiddenVariables)
eventsCompleted.Name = "eventsCompleted"

while wait(0.1)do
    local playerUserId = "Player_"..player.UserId

    local data
    local success, errorMessage = pcall(function()
        data = myDataStore:GetAsync(playerUserId)
    end)

    if success then
        eventsCompleted.Value = data 
    end     
end     

end)

0
Trying to save every tenth of a second will result in data loss. qVoided 221 — 3y

2 answers

Log in to vote
0
Answered by 3 years ago

Do u have API services on? Thats a setting that MUST be turned on for things to save.

0
^ This only applies in studio. Sebgamingkid 147 — 3y
0
ok just wondering Noobmasterscripter 3 — 3y
0
yes, it's on edwisc 1 — 3y
Ad
Log in to vote
-1
Answered by 3 years ago

The main issue that I see is that you are using :GetAsync(). :GetAsync() is used for getting data from the datastore. When the player leaves the game you should use :SetAsync() in order to save their data after they've left.

Fixed script:


local dataStoreService = game:GetService("DataStoreService") local myDataStore = dataStoreService:GetDataStore("myDataStore") local function encodeUserId(player) return "Player_" .. player.UserId end game.Players.PlayerAdded:Connect(function(player) --- Leaderstats Variables --- Hidden Variables local hiddenVariables = Instance.new("Folder", player) hiddenVariables.Name = "hiddenVariables" local eventsCompleted = Instance.new("IntValue",hiddenVariables) eventsCompleted.Name = "eventsCompleted" local playerUserId = encodeUserId(player) local success, errorMessage; while not success do local data success, errorMessage = pcall(function() data = myDataStore:GetAsync(playerUserId) end) if success then eventsCompleted.Value = data else warn(errorMessage) wait(2) end end end) game.Players.PlayerRemoving:Connect(function(player) local playerUserId = encodeUserId(player) local success, errorMessage; while not success do local success, errorMessage = pcall(function() myDataStore:SetAsync(playerUserId, player.hiddenVariables.eventsCompleted.Value) end) if not success then warn(errorMessage) wait(2) end end end)

It also appears that you have only copied part of your script so I cannot guarantee that this will all work.

0
No! Do not use :SetAsync()! It has a high chance of data loss and replaces the oldData instead of considering it. Use :UpdateAsync() instead. qVoided 221 — 3y
0
^ I was correcting a spelling mistake, not endorsing :SetAsync() Sebgamingkid 147 — 3y
0
^ I was correcting a spelling mistake, not endorsing :SetAsync() Sebgamingkid 147 — 3y

Answer this question