DataStore not saving player's items

Oh you know what? It’s probably because I’m making a new BoolValue, and because it’s a server script, it doesn’t even know it’s there because it wasn’t there when the server started.

Edit: nevermind, the easter egg script does this and it works

The saving is working, I figured its just putting it into the backpack that isn’t working

Well the purchased items script has nothing to do with the backpack and it’s still not working

like ive said disable all ur datasaving scripts and only test the script the problem is happening on, it may be from to many setasyncs

I tried disabling the other data stores and it’s still not saving right

do these print? can i see the output

They do not. It just prints “Saved!”
I tried adding code that just prints the names of everything in the folder and it still doesn’t print anything

after a post with 26 comments I found the issue, you need to wait for the players character.

local DataStoreService = game:GetService("DataStoreService")

local DataStore = DataStoreService:GetDataStore("EquippedUserItems")

local ReplicatedStorage = game:GetService("ReplicatedStorage")

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function()
		local data

		local success, errorMsg = pcall(function()
			data = DataStore:GetAsync("EquippedUserItems-"..player.UserId)
		end)
		
		if success and data then
			for _, itemName in pairs(data) do
				
				local sword = game:GetService("ReplicatedStorage"):WaitForChild("ShopItems").Items[itemName]:Clone()
				sword.Parent = player.Backpack
			end
		end
	end)
	
end)

game.Players.PlayerRemoving:Connect(function(player)
	local itemTable = {}
	for _, item in pairs(player.Backpack:GetChildren()) do
		table.insert(itemTable,item.Name)
		print(item.Name.. " saved to UserId "..player.UserId)
	end
	
	local success, errorMsg = pcall(function()
		DataStore:SetAsync("EquippedUserItems-"..player.UserId,itemTable)
	end)

	if success then
		print("Saved!")
	else
		print(errorMsg)
	end
end)

game:BindToClose(function()
	task.wait(2)
end)

this should work

1 Like

Does this look okay?

local DataStoreService = game:GetService("DataStoreService")

local DataStore = DataStoreService:GetDataStore("EquippedUserItems")

local ReplicatedStorage = game:GetService("ReplicatedStorage")

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function()
		local data

		local success, errorMsg = pcall(function()
			data = DataStore:GetAsync("EquippedUserItems-"..player.UserId)
		end)

		if success and data then
			for _, itemName in pairs(data) do
				game:GetService("ReplicatedStorage"):WaitForChild("ShopItems").Items[itemName]:Clone().Parent = player.Backpack
			end
		end
		player.CharacterRemoving:Connect(function(char)
			char.Humanoid:UnequipTools()
		end)
	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
	local plrbackpack = player:FindFirstChild("Backpack")
	local itemTable = {}
	for _, item in pairs(plrbackpack:GetChildren()) do
		table.insert(itemTable,item.Name)
		print(item.Name.. " saved to UserId "..player.UserId)
	end
	local success, errorMsg = pcall(function()
		DataStore:SetAsync("EquippedUserItems-"..player.UserId,itemTable)
	end)
	
	if success then
		print("Saved!")
	else
		print(errorMsg)
	end
end)

game:BindToClose(function()
	task.wait(2)
end)

yes, that looks perfectly fine.

That did not fix it, and I think I know why. When the player is in the shop, it makes a new BoolValue in a LocalScript.

local new = Instance.new("BoolValue")
new.Name = PublicItemID
new.Parent = player.UserItems

My guess is there’s some client/server thing going on here. I could be dead wrong though xD
Edit: The item is also given to the player through the same script.
Edit 2: I’m gonna try to make an event that gives this BoolValue to the player through a server script in ServerScriptService

It still doesn’t explain the currently equipped items though

if ur putting a bool value thru client and server just use replicated storage

Ah mb, I meant to say ReplicatedStorage

1 Like

Alright, so the player items are now working! It’s still not giving the player their items back when they rejoin, but I can go into the shop and equip it. I’ll take a look around to see if I can fix this myself. Thanks so much guys! If I have any more problems with this, I’ll reply here again lol.
Or maybe I should just create a new thread, it doesn’t really link to my main issue.

1 Like

it worked for me, its something to do with another script

1 Like