Datastore Problems

Hi, First of all I am a new scripter and I was following @Alvin_Blox 's tutorial.
After I finished the game I tested it but surprise surprise, I got a problem with Datastores.

The amount of Cash I have saves, but the weapons that I buy from the shop does not save.

what have I done to try and solve this?

  • I looked it up but found nothing.
  • I have read over my scripts countless times
  • checked the output, (nothing there)
  • I even sent hours following the tutorial again to make game again on empty baseplate (the same thing happen)
  • Its not on a group

So I didn’t want to make this post, but I don’t have a choice.

the part of my code that saves the data

-- Data Stores

local player_data 
local weaponsData
local equippedData

pcall(function()
	player_data = dataStores:GetAsync(player.UserId.."-Coins")
end)

pcall(function()
	weaponsData = dataStores:GetAsync(player.UserId.."-Weps")
end)

pcall(function()
	equippedData = dataStores:GetAsync(player.UserId.."-EquippedValue")
end)

if player_data ~= nil then
	-- Player has saved data, load it in
	Coins.Value = player_data
else
	-- New player
	Coins.Value = defaultCash
end

if weaponsData then
	
	for _, weapon in pairs (weaponsData)do
		if game.ServerStorage.Items:FindFirstChild(weapon)then
			local weaponClone = game.ServerStorage.Items[weapon]:Clone()
			weaponClone.Parent = inventory
			print (weapon.." loaded in")
		end
	end
	
	if equippedData then
		equipped.Value = equippedData
		player:WaitForChild("PlayerGui")
		game.ReplicatedStorage.SendEquipped:FireClient(player,equippedData)
	end
	
	
else 
	print("No weapons Data")
end
1 Like

Can you add a print after checking if weaponsData exists?

1 Like

I did, and i think we might be on to something because I found an Error in the

I have a feeling that this is a Roblox issue, not with the script. what do you think?

Depending on how you set it up, saving wont work in studio. Test it in-game and if it works than you’re fine.

that’s what I did, I published the game and tested it with an alt but it didn’t work. I checked the console for Errors and I found the one above in the screenshot.

Check to see if your if statement is true

game.ServerStorage.Items:FindFirstChild(weapon)

try printing that in the loop to check if it’s nil

I already tried putting prints in loops, and I keep getting that error in the screenshot.
LoadChatInfoInternal had an error: HTTP 503 (service unavailable)

If weaponData is nil then you have a saving issue, make you put in the wrong key or just saved the wrong data

1 Like

That error is something else btw

1 Like

Ok ill go over all my scripts and see if i saved the wrong key or not.

here is the script, I checked but found nothing.

local bindableEvent = Instance.new(“BindableEvent”)

game.Players.PlayerRemoving:Connect(function(player)

pcall(function()
	dataStores:SetAsync(player.UserId.."-Coins",player.leaderstats.Coins.Value)
end)

pcall(function()
	local weapons = game.ServerStorage.PlayerData[player.Name].Inventory:GetChildren()
	local weaponsTable = {}
	
	for _, v in pairs (weapons) do
		table.insert(weaponsTable,v.Name)
	end
	
	dataStores:SetAysnc(player.UserId.."-Weps",weaponsTable)
	
	if game.ServerStorage.PlayerData[player.Name].Equipped.Value ~= nil then
		local equippedVal = game.ServerStorage.PlayerData[player.Name].Equipped
		dataStores:SetAysnc(player.UserId.."-EquippedValue",equippedVal.Value)
		print("worked")
	end
end)

print("Saved Weapons and Points")

playersLeft = playersLeft - 1
bindableEvent:Fire()

end)

game:BindToClose(function()
– This will be triggered upon shutdown
while playersLeft > 0 do
bindableEvent.Event:Wait()
end

end)

Could you add this after it puts the weapons in the weaponsTable and tell me what it says

print(table.concat(weaponsTable, ', '))

trying to figure out if the weaponsTable is empty when being saved

1 Like

Ok Give me a second while I test.

	for _, v in pairs (weapons) do
		table.insert(weaponsTable,v.Name)
		print(table.concat(weaponsTable, ', '))
	end

No, put it after the loop, not in it

1 Like

Then tell me what the output says

1 Like

like this?

game.Players.PlayerRemoving:Connect(function(player)

pcall(function()
	dataStores:SetAsync(player.UserId.."-Coins",player.leaderstats.Coins.Value)
end)

pcall(function()
	local weapons = game.ServerStorage.PlayerData[player.Name].Inventory:GetChildren()
	local weaponsTable = {}
	
	for _, v in pairs (weapons) do
		table.insert(weaponsTable,v.Name)
	end
	print(table.concat(weaponsTable, ', '))
            dataStores:SetAysnc(player.UserId.."-Weps",weaponsTable)
	
	if game.ServerStorage.PlayerData[player.Name].Equipped.Value ~= nil then
		local equippedVal = game.ServerStorage.PlayerData[player.Name].Equipped
		dataStores:SetAysnc(player.UserId.."-EquippedValue",equippedVal.Value)

	end
end)

Yes that’s exactly it :slight_smile:

1 Like

Ok thank you time to test then.