Datastore keeps resetting values to 0

Hi

I have made a datastore script, which holds multiple IntValues. It has been working for a while, but suddenly all the values have been resetting to 0.

Script:

local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local RS = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvents")
local LoadData = RS:FindFirstChild("LoadData")
local playerData = DataStoreService:GetDataStore("PFStores")

local function onPlayerJoin(player)  
	
	local Folder = Instance.new("Folder" , player)  
	Folder.Name = "Settings"
	
	local FOV = Instance.new("IntValue" , Folder)
	FOV.Name = "FOV"
	
	local Time = Instance.new("IntValue" , Folder)
	Time.Name = "Time"
	
	local Brightness = Instance.new("IntValue" , Folder)
	Brightness.Name = "Brightness"
	
	local Cooldown = Instance.new("IntValue" , Folder)
	Cooldown.Name = "Cooldown"
	
	local Seconds = Instance.new("IntValue" , Folder)
	Seconds.Name = "Seconds"
	
	
	local playerUserId = "Player_" .. player.UserId 
	local data = playerData:GetAsync(playerUserId) 

	
	
	if data then
		
		
		FOV.Value = data[1]
		Time.Value = data[2]
		Brightness.Value = data[3]
		Cooldown.Value = data[4]
		Seconds.Value = data[5]
		LoadData:FireClient(player)
		
		

	else
		
		FOV.Value = 70
		Time.Value = 12
		Brightness.Value = 1
		Cooldown.Value = 0.075
		Seconds.Value = 0
		LoadData:FireClient(player)
	end
end


local function create_table(player)

	local player_stats = {}
	
	for _, stat in pairs(player.Settings:GetChildren()) do
		player_stats[stat.Name] = stat.Value
	end
	
	return player_stats
end


local function onPlayerExit(player)  
	
	local player_stats = create_table(player)
	
	local success, err = pcall(function()
		local playerUserId = "Player_" .. player.UserId
		playerData:SetAsync(playerUserId, player_stats) 
	end)
	
	if not success then
		warn('There was a DataStore error: ' .. err)
	end
end


Players.PlayerAdded:Connect(onPlayerJoin)
Players.PlayerRemoving:Connect(onPlayerExit)

Any help would be appriciated, thanks!

The problem I see when looking at your code is that you are saving like this:

[key] = value

Which is (ex for one of your values)

Table["Brightness"] = 1

However, when you are loading the data, the table looks like:

{
    "Brightness" = 1
}

What you are doing is trying to get Table[1], Table[2]. This won’t work because the keys in the table are strings and not by indexes.

Therefore, in your loading function, data[1] = nil, data[2] = nil, etc.

The values are being seen as 0 because when a number value is set to a different type (string, bool, etc), it is attempting to convert the value you provide into a number or integer. In this case, nil cannot be converted into a number, or is logically 0.

To fix this, you must get the data the same way you are saving it:

if data then
	FOV.Value = (data['FOV'] or 70);
	Time.Value = (data['Time'] or 12);
	Brightness.Value = (data['Brightness'] or 1);
	Cooldown.Value = (data['Cooldown'] or 0.075);
	Seconds.Value = (data['Seconds'] or 0);

	LoadData:FireClient(player)
end
2 Likes