Saving tables wont work

I need to save and load peoples guns. But it doesn’t load
image

script:

local DataStoreService = game:GetService("DataStoreService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local HTTPService = game:GetService("HttpService")
local Players = game:GetService("Players")

local playerData = DataStoreService:GetDataStore("PlayerData_1")
local Tools = ServerStorage.Tools
local Events = ReplicatedStorage.Events
local Functions = ReplicatedStorage.Functions

local function SetupGuns()
	for _, gun in pairs(Tools:GetChildren()) do
		local serverScript = script.Server:Clone()
		local clientScript = script.Client:Clone()

		serverScript.Parent = gun.Handle
		clientScript.Parent = gun.Handle

		serverScript.Enabled = true
		clientScript.Enabled = true
	end
end

local function savePlayerData(player)
	local key = "Player_" .. player.UserId

	local dataToSave = {
		Cash = player.Cash.Value or 1000,
		Guns = HTTPService:JSONEncode(player.Guns:GetChildren() or {})
	}
	
	print("Saving Cash: " .. dataToSave.Cash)
	print("Saving Guns: " .. tostring(HTTPService:JSONDecode(dataToSave.Guns)))

	local success, errorMessage = pcall(function()
		playerData:SetAsync(key, dataToSave)
	end)

	if not success then
		warn("Failed to save data for " .. player.Name .. ": " .. errorMessage)
	end
end

local function loadPlayerData(player)
	local key = "Player_" .. player.UserId

	local success, data = pcall(function()
		return playerData:GetAsync(key)
	end)

	if success and data then
		if player:FindFirstChild("Cash") then
			player.Cash.Value = data.Cash
		end

		if player:FindFirstChild("Guns") then
			print("Has guns")
			print("Guns: " .. data.Guns)
			for _, gunData in pairs(HTTPService:JSONDecode(data.Guns)) do
				print(gunData)
				local gun = Instance.new("StringValue")
				gun.Name = gunData.Name
				gun.Parent = player.Guns
			end
		end
	else
		warn("Failed to load data for " .. player.Name)
	end
end

local function purchaseGun(player, gunName)
	local tool = Tools:FindFirstChild(gunName)

	if tool then
		local cash = player:FindFirstChild("Cash")

		if cash and cash.Value >= tool.Price.Value then
			cash.Value = cash.Value - tool.Price.Value

			local gunClone = tool:Clone()
			gunClone.Parent = player.Backpack

			local gunsFolder = player:FindFirstChild("Guns")

			local gunData = Instance.new("StringValue")
			gunData.Name = gunName
			gunData.Parent = gunsFolder

			return true
		else
			return false
		end
	else
		warn("Gun not found: " .. gunName)
		return false
	end
end

Functions.PurchaseGun.OnServerInvoke = function(player, gunName)
	return purchaseGun(player, gunName)
end

Players.PlayerAdded:Connect(function(player)
	local cash = player:FindFirstChild("Cash")
	if not cash then
		cash = Instance.new("IntValue")
		cash.Name = "Cash"
		cash.Value = 1000
		cash.Parent = player
	end

	local gunsFolder = player:FindFirstChild("Guns")
	if not gunsFolder then
		gunsFolder = Instance.new("Folder")
		gunsFolder.Name = "Guns"
		gunsFolder.Parent = player
	end

	player.CharacterAdded:Connect(function(character)
		loadPlayerData(player)
	end)
end)

SetupGuns()

Players.PlayerRemoving:Connect(function(player)
	savePlayerData(player)
end)

game:BindToClose(function()
	for _, player in pairs(Players:GetPlayers()) do
		savePlayerData(player)
	end
end)

You can’t save instances to DataStore,
You should loop through table


as we see it turns into null, since it’s instance
proper way of saving your guns would be

local tab = {}
for _,gun in pairs(player.Guns:GetChildren()) do
table.insert(tab,{Name = gun.Name, Ammo = gun.Ammo.Value} --whatever
end
dataToSave.Guns = tab

what about loading the guns data?

for _, gunData in pairs(data.Guns) do

Failed to load data for mlnitoon2

use prints?

where ?

[[[extracharactersss]]]

You want to find out what happens to the Guns,
so I would prefer you to use Prints to check what’s going on with the Guns,

I feel like your Player is disconnecting before the script can get all guns.

The only gun a player starts with has only 5 parts, 3 of which are foreffects.

its still not working.

extracharacrers