DataStore error while loading out and in studio

While using the Developer Hub on scripting a DataStore, i’ve encountered , besides an error on saving data, some different errors which didn’t appeared before i made the datastore. What i want to do is to save a player’s money, level and items (if he buys them) in a datastore and , as i said, i got extremely worried about the amount of errors i got from one script.

Anyway here is some information about my problem

Here are the errors i mentioned above. As you can see i get the errors that my data didn’t load.
image

The script i used DataStore in, so i can save a player’s money , script situated in ServerScriptService

local playerDSS = DataStoreService:GetDataStore("DSS")

game.Players.PlayerAdded:Connect(function(player)
	local stats = Instance.new("Folder", player)
	stats.Name = "leaderstats"
	
	local vall = Instance.new("IntValue", stats)
	vall.Name = "Cash"
	vall.Value = 0
	
	spawn(function()
		while true do
			vall.Value = vall.Value + 1
			wait(60)
		end
	end)
		
	vall:GetPropertyChangedSignal("Value"):Connect(function() --Function will run every time Value is changed, meaning either an increase or decrease in cash.
		local cashNumber = player.PlayerGui:WaitForChild("MenuInterface"):WaitForChild("Menu"):WaitForChild("ProfileButton"):WaitForChild("ProfileMainFrame"):WaitForChild("SecFrame"):WaitForChild("CashNumber")

		cashNumber.Text = vall.Value
	end)
	local data
	local success,errormessage = pcall(function()
		data = playerDSS:GetAsync(player.UserId.."-cash")
		end)
	if success then
		cash.Value = data
	else
		print("Error loading data!")
		warn(errormessage)
	end
end)

game.Players.PlayerAdded:Connect(function(player)
	local success, errormessage = pcall(function()
		playerDSS:SetAsync(player.UserId.."-cash",player.leaderstats.Cash.Value)
		end)
	if success then
		print("Data successfuly saved!")
	else
		print("Error saving data!")
		warn(errormessage)
	end
end)

Thank you for helping and explaining me <3

You haven’t make DataStoreService a variable. Add this line to the top:
local DataStoreService = game:GetService("DataStoreService")

Also make sure to enable studio access to API services.

As FlashFlame stated you need to make the DS a Variable.

local Ds = game:GetService("DataStoreService"):GetDataStore("Name of DataStore")

Also API needs to be enabled too for accessing them.

1 Like

Okay but what about the module error, or that is caused by the DataStore error?

That’s just the plugin.
(30chars)

I did what you and @Snoopy1333 said and i still get these errors
image

Also i noticed that “cash” in my script is unknown. I tried moving the whole function around and cash is still unknown.

local Ds = game:GetService("DataStoreService"):GetDataStore("playerDSS")

game.Players.PlayerAdded:Connect(function(player)
	local stats = Instance.new("Folder", player)
	stats.Name = "leaderstats"
	
	local vall = Instance.new("IntValue", stats)
	vall.Name = "Cash"
	vall.Value = 0
	
	spawn(function()
		while true do
			vall.Value = vall.Value + 1
			wait(60)
		end
	end)
		
	vall:GetPropertyChangedSignal("Value"):Connect(function() --Function will run every time Value is changed, meaning either an increase or decrease in cash.
		local cashNumber = player.PlayerGui:WaitForChild("MenuInterface"):WaitForChild("Menu"):WaitForChild("ProfileButton"):WaitForChild("ProfileMainFrame"):WaitForChild("SecFrame"):WaitForChild("CashNumber")

		cashNumber.Text = vall.Value
	end)
	local data
	local success,errormessage = pcall(function()
		data = Ds:GetAsync(player.UserId.."-cash")
		end)
	if success then
		cash.Value = data
	else
		print("Error loading data!")
		warn(errormessage)
	end
end)

It says that “cash” is unknown , in cash.Value = data

Check out this video:

This is how i do my Datastores

Well, because cash as a variable simply isn’t declared in that anonymous function. - But vall is (<-- big hint :wink:).

BTW is it discouraged to use the second argument for Instance.new(). Instead set the .Parent much later, as can be seen in this code sample.

Also you should probably avoiding this infinite, never-ending-loop-that-stops-only-when-it-crashes-due-to-error, code:

	spawn(function()
		while true do
			vall.Value = vall.Value + 1
			wait(60)
		end
	end)

Perhaps instead make a RunService, Heartbeat function, that does the “increment cash by 1” for all the players still connected to the game.

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local sumTime = 0
local secondsBetweenCashIncrement = 60
RunService.Heartbeat:Connect(function(dt)
	sumTime = sumTime + dt
	if sumTime > secondsBetweenCashIncrement then
		sumTime = sumTime - secondsBetweenCashIncrement
		for _,ply in ipairs(Players:GetChildren()) do
			local leaderstats = ply:FindFirstChild("leaderstats")
			if leaderstats then
				local cash = leaderstats:FindFirstChild("Cash")
				if cash then
					cash.Value = cash.Value + 1
				end
			end
		end
	end
end)

Instead of

cash.Value = data

You put “vall” as the variable, not “cash”, so try

vall.Value = data

Also, you forgot to do

stats.Parent = player

and

vall.Parent = stats

I watched the video Snoopy1333 showed me and it works just fine but im also thankful to Decker004 for explaining me so much.
With the video @Snoopy1333 showed me and with what @Decker004 told me about increment cash by 1 script i finally managed to save the data after struggling 2 days to understand it from youtube videos and Developer Hub. Also, thank you so much @FlashFlame_Roblox for being here again to help me, since i realised that you’ve helped me in my other topics posted this week.
Even tho Decker and FlashFlame helped me too, i’d give the solution to Snoopy’s video, which helped me understand how DataStore works.

Y’all are awesome <3

2 Likes