Problem with DataStore

Hello, thank you for responding to my previous forums.

I find myself in a not-so-serious but puzzling situation, which I can’t fully grasp due to being quite new to topics related to DataStore.

To begin, here is my server-side script:

local players = game:GetService("Players")
local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("SuccesValueSaver")
local ds2 = datastore:GetDataStore("SuccesValueSaver")
local ds3 = datastore:GetDataStore("SuccesValueSaver")
local ds4 = datastore:GetDataStore("SuccesValueSaver")
local ds5 = datastore:GetDataStore("SuccesValueSaver")
local ds6 = datastore:GetDataStore("SuccesValueSaver")
local ds7 = datastore:GetDataStore("SuccesValueSaver")

local ActualGameVersion = game.ReplicatedFirst:WaitForChild("InGameSettings").GameVersion

players.PlayerAdded:connect(function(player)
	local folder = Instance.new("Folder")
	folder.Name = "PlayerInfo"
	folder.Parent = player
	
	local folder2 = Instance.new("Folder")
	folder2.Name = "FlashlightInfo"
	folder2.Parent = player:WaitForChild("PlayerInfo")
	
	local folder3 = Instance.new("Folder")
	folder3.Name = "PlayerSettings"
	folder3.Parent = player:WaitForChild("PlayerInfo")

    -- PLAYERINFO
	local currency1 = Instance.new("NumberValue")
	currency1.Name = "GameVersion"
	currency1.Parent = player.PlayerInfo
	currency1.Value = ds1:GetAsync(player.UserId) or 0 -- Unable to assign property Value. string expected, got boolean
	ds1:SetAsync(player.UserId, currency1.Value)
	
	local currency2 = Instance.new("IntValue")
	currency2.Name = "LimiCash"
	currency2.Parent = player.PlayerInfo
	currency2.Value = ds2:GetAsync(player.UserId) or 0
	ds2:SetAsync(player.UserId.."LimiCash", currency2.Value) -- Utiliza una clave única para LimiCash
	
	local currency3 = Instance.new("IntValue")
	currency3.Name = "Deaths"
	currency3.Parent = player.PlayerInfo
	currency3.Value = ds3:GetAsync(player.UserId) or 0
	ds3:SetAsync(player.UserId, currency3.Value)
	
	local currency4 = Instance.new("IntValue")
	currency4.Name = "LevelsPlayed"
	currency4.Parent = player.PlayerInfo
	currency4.Value = ds4:GetAsync(player.UserId) or 0
	ds4:SetAsync(player.UserId, currency4.Value)
	-----------------------------------------------------------
	-- PLAYERSETTINGS
	
	local currency5 = Instance.new("BoolValue")
	currency5.Name = "InfoFrame"
	currency5.Parent = player.PlayerInfo.PlayerSettings
	currency5.Value = ds5:GetAsync(player.UserId) or 0
	ds5:SetAsync(player.UserId, currency5.Value)
	
	local currency6 = Instance.new("BoolValue")
	currency6.Name = "LobbyMusic"
	currency6.Parent = player.PlayerInfo.PlayerSettings
	currency6.Value = ds6:GetAsync(player.UserId) or 0
	ds6:SetAsync(player.UserId, currency6.Value)
	
	local currency7 = Instance.new("BoolValue")
	currency7.Name = "GlobalShadows"
	currency7.Parent = player.PlayerInfo.PlayerSettings
	currency7.Value = ds6:GetAsync(player.UserId) or 0
	ds6:SetAsync(player.UserId, currency7.Value)
	-----------------------------------------------------------
	
	currency1.Changed:connect(function()
		ds1:SetAsync(player.UserId, currency1.Value)
	end)
	
	currency2.Changed:connect(function()
		ds2:SetAsync(player.UserId, currency2.Value)
	end)
	
	currency3.Changed:connect(function()
		ds3:SetAsync(player.UserId, currency3.Value)
	end)
	
	currency4.Changed:connect(function()
		ds4:SetAsync(player.UserId, currency4.Value)
	end)
	
	currency5.Changed:connect(function()
		ds5:SetAsync(player.UserId, currency5.Value)
	end)
	
	currency6.Changed:connect(function()
		ds6:SetAsync(player.UserId, currency6.Value)
	end)
	
	currency7.Changed:connect(function()
		ds7:SetAsync(player.UserId, currency7.Value)
	end)
end)

while true do
	for _, v in pairs(game.Players:GetPlayers()) do
		v.PlayerInfo.GameVersion.Value = ActualGameVersion.Value
	end
	wait(2)
end

In this case, I’ve created three different variables, each unrelated to the other. Let’s say I earn 100 LimiCash in the game; after that, I exit the game and re-enter. Well, all the variables end up with the same value as the earned LimiCash.

Image of my earned 100 LimiCash:

image

Image of how, after exiting and re-entering the game, I get all values equal:

image

I hope you can help me with this small issue. I’ve also found several errors in the script that, if you see or know them, could you help me correct them? Thank you for your attention. :slight_smile:

You’re getting the same DataStore, but using 7 different variables.

Always use a pcall() when accessing the DataStore.

Try not using an or statement. Check if it’s nil before assigning a default value.

I don’t know what is the exact solution to my problem…

It is a completely wrong usage of data stores:

  • Creating different variables with same datastore name will not access all your values. You just access the same data store seven times.

  • .Changed connection may flood the SetAsync until it fully stops work.

  • Always wrap data store requests into pcall since it can occasionally fail and stop the rest of script from work.

  • You should create a single data store and save inside of it a file with all player’s information.

I recommend you to use ProfileService module for best results with data work, but I don’t guarantee that it will be clearly understandable for your scripting level.

You can send me an example or something else??..

Looking at it makes me want to die :sob::sob::sob:

1 Like