Data store wont work?

im creating a shop system where you only have to buy items once and you’ll have it forever. Im having some trouble with the scripting though.

players.PlayerAdded:Connect(function(plr)
	------------------
	--Variables
	local UserID = "Customer_"..plr.UserId
	local folder = Instance.new("Folder", SS["Player Owned Assets"])
	folder.Name = UserID
	------------
	--Give player knife value
	local HasKnife = Instance.new("BoolValue",SS["Player Owned Assets"]:WaitForChild("Customer_"..plr.UserId))
	HasKnife.Name = "HasKnife"
	
	
	
	--Loading data
	local data
	local getSuccess, eMessage = pcall(function()
		data = shopStore:GetAsync(UserID) --Putting the saved data somewhere (A variable)
	end)
	
	if getSuccess then
		print("there is shop data")
		HasKnife.Value = data.KnifeValue
	else
		print("nope")
		warn(eMessage)
	end
end)

players.PlayerRemoving:Connect(function(plr)
	---------------------
	--Variables
	local UserID = "Customer_"..plr.UserId
	local folder = FolderObject:FindFirstChild("Customer_"..plr.UserId)
	local HasKnife = folder.HasKnife
	local data = {
		KnifeValue = HasKnife.Value
	}
	----------------
	--Saving Data
	local getSuccess, eMessage = pcall(function()
		data = shopStore:SetAsync(UserID) --Putting the saved data somewhere (A variable)
	end)
	
	if getSuccess then
		print("Shop Data Saved Sucessfully")
		HasKnife.Value = data.KnifeValue
	else
		warn(eMessage)
	end
end)

for some reason, its saying that “HasKnife” is nil. I added a watch for child but that didnt seem to work. Here are the error messages

17:13:50.130 ServerScriptService.Scripts.Default.Data Handler:90: attempt to index nil with 'KnifeValue' - Server - Data Handler:90

17:14:00.317 Argument 2 missing or nil - Server

help would greatly be apprciated. thank you for your time

are you sure that HasKnife is in the folder, maybe it isnt created by the point where your script “wants” it?

1 Like

I originally created the HasKnifevalue in a seperate script under a players added event. I moved it to this script so im sure its created before the data is saved/loaded. Im positive its created so im not exactly sure what the issue is

i am currently rewriting the script entirely trying to find an issue

ok my next approche would be a :WaitForChild() but maybe the rewrite will fix your issue

I presume this error is occurring when leaving the game? If this is the case it is because you are storing the response that SetAsync is giving you which is invalid. SetAsync does not return the saved data.

https://developer.roblox.com/en-us/api-reference/function/GlobalDataStore/SetAsync

heres teh rewritten script, it still didnt work

local players = game.Players
local DSS = game:GetService("DataStoreService")
local ShopStorage = DSS:GetDescendants("ShopStorage")

players.PlayerAdded:Connect(function(plr)
	-----------------
	--Variables
	local ID = "Customer_"..plr.UserId
	local folder = game:GetService("ServerStorage"):WaitForChild("Player Owned Assets")
	local OwnedItems = folder:WaitForChild("Customer_"..plr.UserId)
	local HasKnife = OwnedItems:WaitForChild("HasKnife")
	print(HasKnife.Value)
	----------------
	--Loading Data
	local data
	local getSuccess, eMessage = pcall(function()
		data = ShopStorage:GetAsync(ID) --Putting the saved data somewhere (A variable)
	end)
	if getSuccess then
		if data then
			print("Theres Shop data")
			HasKnife.Value = data
		else
			print("There is no Shop data")
		end
	else
		warn(eMessage)
	end
end)

players.PlayerRemoving:Connect(function(plr)
	local ID = "Customer_"..plr.UserId
	local folder = game:GetService("ServerStorage")["Player Owned Assets"]
	local OwnedItems = folder:WaitForChild("Customer_"..plr.UserId)

	local HasKnife = OwnedItems:WaitForChild("HasKnife")
	local data = HasKnife.Value
	
	local success, errored = pcall(function()
		ShopStorage:SetAsync(ID, data)
	end)
	
	if success then
		print("Shop Data saved")
	end
	if errored then  
		warn(errored)
	end
end)

Do you have other data store scripts working in your game?

1 Like

yeah, other datastores work spoiler[/spoiler]

Its because you aren’t saving the data.

Change the line to this:

data = shopStore:SetAsync(UserID,HasKnife.Value)
2 Likes