Datastore2 not saving character store

hello im trying to make datastore2 save characters like Defend The Train but when i do that it doesnt actually save and just set it to Hat

this is the code

local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")
local Workspace = game:GetService("Workspace")
local Hats = game:GetService("ReplicatedFirst"):WaitForChild("Hats")
local DataStore2 = require(ServerScriptService.DataStore2)
-- Combine every key you use. This will eventually be the default, but for now read the "Gotchas" section to understand why we need this.
DataStore2.Combine("loldata", "points")
DataStore2.Combine("loldata","char")
Players.PlayerAdded:Connect(function(player)
	local DefaultCharacter = {
		Hats.Hat, --hat 1
		nil, -- hat 2
		nil, -- hat 3
		nil, -- torso color
		nil -- shirt graphic
	}
	local pointsStore = DataStore2("points", player)
    local characterstore = DataStore2("char",player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	local characterdata = characterstore:Get(DefaultCharacter)
	local Char = player.Character
	print('Loading character...')
	for i,v in ipairs(characterdata) do
		print(i)
		print(v)
		if v ~= nil then
			if v:IsA('Accessory') then
				v:Clone().Parent = Char
				if i == 4 then
					Char:WaitForChild("Torso").Color = v
				elseif i == 5  then
					Char:WaitForChild("Shirt Graphic").Graphic = v
				end
			end
		end
	end
	local points = Instance.new("NumberValue")
	points.Name = "Points"
	points.Value = pointsStore:Get(0) -- The "0" means that by default, they'll have 0 points
	points.Parent = leaderstats
	pointsStore:OnUpdate(function(newPoints)
		-- This function runs every time the value inside the data store changes.
		points.Value = newPoints
	end)
	characterstore:OnUpdate(function(newChar)
		if newChar ~= characterdata then
	    characterdata = newChar
		for i,v in ipairs(Char:GetChildren()) do
			if v:IsA("Accessory") then
				v:Destroy()
			end
		end 
		print('Loading character...')
		--characterstore:Set(characterdata)
		for i,v in ipairs(characterdata) do
			print(i)
			print(v)
			if v ~= nil then
				if v:IsA('Accessory') then
					v:Clone().Parent = Char
					if i == 4 then
						Char:WaitForChild("Torso").Color = v
					elseif i == 5  then
						Char:WaitForChild("Shirt Graphic").Graphic = v
					end
				end
			end
			end
			end
	end)
	leaderstats.Parent = player
	task.defer(function()
		while task.wait(100) do
			characterdata:Set(characterdata)
		end
	end) 
end)
Players.PlayerRemoving:Connect(function(player)
	local characterstore = DataStore2("char",player)
	local characterdata = characterstore:Get()
	characterstore:Set(characterdata)
	print(characterdata)
end)

EDIT: i forgot to add the hat giver

task.wait(10)
local ServerScriptService = game:GetService("ServerScriptService")
local DataStore2 = require(ServerScriptService.DataStore2)
DataStore2.Combine("loldata","char")
local player = game.Players.pumpkyrofl
local characterstore = DataStore2("char",player)
local characterdata = characterstore:Get()
workspace.duck.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChildOfClass("Humanoid") then
		local clone = table.clone(characterdata)
		clone[1] = game.ReplicatedFirst.Hats.RubberDuckie
		characterstore:Set(clone)
	end
end)
workspace.none.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChildOfClass("Humanoid") then
		local clone = table.clone(characterdata)
		clone[1] = game.ReplicatedFirst.Hats.Hat
		characterstore:Set(clone)
	end
end)

strangely im still trying to figure the issue out it still doesnt save… what is causing this…
also bump.

are you getting an error on your output?

no theres no errors it just says everything is fine but when i join it back it sets my accessory back to Hat instead of the rubbie duckie

nevermind the console says error when player left! Invalid at input.char.1 because: Invalid type (Instance)

You cannot directly save objects to datastores. Does this module helps to save objects to datastores ? Does this module have functions that make easier to use datastores?

its datastore2 How to use DataStore2 - Data Store caching and data loss prevention
but the actual issue is that it doesnt save when i try to do so, which means my character will reset to default

is there a way to fix

error when player left! Invalid at input.characterok.1 because: Invalid type (Instance) 

thats whats causing the data problems probably…
bump.
the script i heavily editted:

local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")
local Workspace = game:GetService("Workspace")
local Hats = game:GetService("ReplicatedFirst"):WaitForChild("Hats")
local DataStore2 = require(ServerScriptService.DataStore2)
-- Combine every key you use. This will eventually be the default, but for now read the "Gotchas" section to understand why we need this.
DataStore2.Combine("ughdata", "points")
DataStore2.Combine("ughdata","characterok")
local DefaultCharacter = {
	Hats.Hat, --hat 1
	nil, -- hat 2
	nil, -- hat 3
	nil, -- torso color
	nil -- shirt graphic
}
Players.PlayerAdded:Connect(function(player)
	local pointsStore = DataStore2("points", player)
	local characterstore = DataStore2("characterok",player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	local characterdata = characterstore:GetTable(DefaultCharacter)
	local Char = player.Character or player.CharacterAdded:Wait()
	print('Loading character...')
	for i,v in ipairs(characterdata) do
		print(i)
		print(v)
		if v ~= nil then
			if v:IsA('Accessory') then
				v:Clone().Parent = Char
				if i == 4 then
					Char:WaitForChild("Torso").Color = v
				elseif i == 5  then
					Char:WaitForChild("Shirt Graphic").Graphic = v
				end
			end
		end
	end
	local points = Instance.new("NumberValue")
	points.Name = "Points"
	points.Value = pointsStore:Get(0) -- The "0" means that by default, they'll have 0 points
	points.Parent = leaderstats
	pointsStore:OnUpdate(function(newPoints)
		-- This function runs every time the value inside the data store changes.
		points.Value = newPoints
	end)
	characterstore:OnUpdate(function(newChardata)
		characterdata = newChardata
			for i,v in ipairs(Char:GetChildren()) do
				if v:IsA("Accessory") then
					v:Destroy()
				end
			end 
			print('Loading character...')
			--characterstore:Set(characterdata)
			for i,v in ipairs(characterdata) do
				print(i,v)
				if v ~= nil then
					if v:IsA('Accessory') then
						v:Clone().Parent = Char
						if i == 4 then
							Char:WaitForChild("Torso").Color = v
						elseif i == 5  then
							Char:WaitForChild("Shirt Graphic").Graphic = v
						end
					end
				end
			end
	end)
	leaderstats.Parent = player
	task.defer(function()
		while task.wait(100) do
			characterstore:Set(characterdata)
		end
	end) 
end)
1 Like