How do i fix "tables cannot be cyclic"

How do i fix this problem?

local dataStore2 = require(game.ServerStorage:WaitForChild("DataStore2"))
local Event = game.ReplicatedStorage.Events:WaitForChild("TalkToClient")
game.Players.PlayerAdded:Connect(function(player)
local InvStore = dataStore2("Inventory", player)
Event:FireClient(player, InvStore) -- This part is saying the error
end)

That error means that you’re referencing a table that you’re storing data in inside of itself. It’s a problem because Roblox encodes tables into a json format for sending across the network and decodes them back into Lua tables at the other end, but json can’t store tables with references to themselves. You’d need to either look at how you’re storing data in InvStore and remove the cyclic table or construct a new table with all the data that you want to send.

5 Likes
local DataStore2 = require(script.Parent.DataStore2)

game:GetService("Players").PlayerAdded:Connect(function(player: Player)
	local store = DataStore2("aaa", player)
	print(store)
end)

When I print this out:

image

That means each datastore2 object’s savingMethod field has a dataStore2 field which likely points to itself.

You shouldn’t be sending the datastore2 object to the client anyway – data stores are server-only and so is this module.

So then how would i update this?

local replicatedstorage = game:GetService("ReplicatedStorage")
local armormodule = require(game.ReplicatedStorage:WaitForChild("ArmorHandler"))


replicatedstorage.Events.UpdateInventory.OnClientEvent:Connect(function(player, armor)
	local template = script.Parent.MainGui.Inventory.Templates.Template
	local newTemplate = template:Clone()
	local armorname = armor.Name
	newTemplate.Parent =  script.Parent.MainGui.Inventory.Duplicates
	newTemplate.Visible = true

	local newArmor = armormodule.chooseRandom():Clone()

	local camera = Instance.new("Camera")
	camera.CFrame = CFrame.new(newArmor.PrimaryPart.Position + (newArmor.PrimaryPart.CFrame.lookVector * 3), newArmor.PrimaryPart.Position)
	camera.Parent = newTemplate.ViewportFrame
	newArmor.Parent = newTemplate.ViewportFrame

	newTemplate.ViewportFrame.CurrentCamera = camera
end)

Would i use a remoteevent or something?

Do you really need to send the datastore2 object to the client? The methods will not be sent. You could probably just do that if you want to send its data

Event:FireClient(player, InvStore:Get({a = 0, b = 0})) -- the default values inside of Get

Im basically trying to do this but with different gui objects