Is this working? - DataStore

What do I want to achieve?
I want to store intValue in the Data Store successfully.

What is my problem!
Well, I am not sure that my code is working correctly or there is some mistakes I made.

The code that I wrote:


local Players = game:GetService("Players")
local DSS = game:GetService("DataStoreService")
local CStore = DSS:GetDataStore("CSystemStorage")

Players.PlayerAdded:Connect(function()

    local CFile = Instance.new("Folder")
	CFile.Name = "CSystem"
	CFile.Parent = player
	
	local Copper = Instance.new("IntValue")
	Copper.Name = "Copper"
	Copper.Value = 0
	Copper.Parent = player.CSystem
	
	local Silver = Instance.new("IntValue")
	Silver.Name = "Silver"
	Silver.Value = 0
	Silver.Parent = player.CSystem
	
	local Gold = Instance.new("IntValue")
	Gold.Name = "Gold"
	Gold.Value = 0
	Gold.Parent = player.CSystem

	local CS
	local SS
	local GS

	local Success , errorM = pcall(function()
	CS = CStore:GetAsync(player.UserId.."-Copper")
	SS = CStore:GetAsync(player.UserId.."-Silver")
	GS = CStore:GetAsync(player.UserId.."-Gold")
	end)
	if Success then
		print(player.Name.." - Your data is in good condition.")
		Copper.Value = CS
		Silver.Value = SS
		Gold.Value = GS
	else
		print(player.Name.." - Whoops! I think you might not have your data saved!")
		warn(errorM)
	end
	
end)

Players.PlayerRemoving:Connect(function(player)
	
	-----------------CStore------------------
	local Copper = player.CSystem.Copper.Value
	local Silver = player.CSystem.Silver.Value
	local Gold = player.CSystem.Gold.Value

local Success , errorM = pcall(function()
	CStore:SetAsync(player.UserId.."-Copper",Copper)
	CStore:SetAsync(player.UserId.."-Silver",Silver)
	CStore:SetAsync(player.UserId.."-Gold",Gold)
	end)
	if Success then
		print("C-System has been saved for - "..player)
	else
		print("There was a leak in the Data Storage! - "..player)
		warn(errorM)
	end	
	
	
end)

The Output I get from the RobloxStudio:

Mid0riyama - Your data is in good condition.
16:14:43.512 - Disconnect from 127.0.0.1|64203
16:14:43.515 - ServerScriptService.P-Core:73: attempt to concatenate string with userdata

My problem is this output. Is this working or not I am not sure.

16:14:43.515 - ServerScriptService.P-Core:73: attempt to concatenate string with userdata"

This part is unnecessary, you could simply parent it to CSystem like this

   Copper.Parent = CFile

As you defined that in your code earlier, same thing with the Silver and Gold variables.

Also do not attempt to use multiple DataStores for different values, this could easily exhaust your budget when sending requests for multiple players. Try saving all your data in one table containing all the values instead.

Do something like this


  local stats = {
                   Copper = Copper.Value;
                   Silver = Silver.Value;
                   Gold   = Gold.Value;
                }  

  DataStore:SetAsync(key, stats)

 

You would get the values from the store (essentially, not exactly) like this:

  local stats = DataStore:GetAsync(key)
  local copper, silver, gold = stats.Copper, stats.Silver, stats.Gold

Of course you’d need to wrap the Get call within a pcall() and remember to retry saving operations if they fail.

1 Like

Thanks a lot this helped me a lot. I just realised that I can make dictionary and I can also save dictionary at once but My problem is my script working or not?

as for your issue, you’re attempting to do pretty much what the error states; attempting to concatenate a string with userdata.

Get over it by doing this:

replace 
	print(player.Name.." - Whoops! I think you might not have your data saved!")
with

 print(tostring(player.Name) .." - Whoops! I think you might not have your data saved!")

wherever you concatenate the UserId or UserName, tostring it

  tostring(player.UserId).. "string"

Note that while saving data, anything you want to save will be turned into a string format internally, the key; for instance the UserId, has tostring() called on it too. Anything that is not a string cannot be accepted by Amazon DynamoDB, which is what DataStoreService interfaces with

1 Like

Thanks for the great support you gave me. I really appreciate it.