Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

DataStore is not saving my variables?

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)

1 comment

0

Trying to save every tenth of a second will result in data loss.

2 answers

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

3 comments

0

^ This only applies in studio.

0

ok just wondering

0

yes, it's on

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.

3 comments

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.

0

^ I was correcting a spelling mistake, not endorsing :SetAsync()

0

^ I was correcting a spelling mistake, not endorsing :SetAsync()

This discussion is preserved as it was. New answers, comments, and votes are closed.