Item Giver Has Circular Reference

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I have a local script that has the item, i call a remote event to the server that should put the item inside of the players backpack.

  1. What is the issue? Include screenshots / videos if possible!

i get the error: Attempt to set parent of Players.TyK214 to Players.TyK214.Backpack would result in circular reference

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

Dev fourms and looking online. i have tried placing my print statements to see what went wrong

local script

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local Items = ReplicatedStorage:WaitForChild("Gears"):GetChildren()

local Container = script.Parent.Container
local itemFrame = Container.ItemsFrame

local shopUI = script.Parent
local exit = shopUI.Container.Exit

local player = Players.LocalPlayer

local PurchaseSound = shopUI.Sounds.Purchase
local ErrorSound = shopUI.Sounds.Error

function updateItems()
	for i, item in pairs(Items) do
		-- Find any old buttons
		local oldButton = itemFrame:FindFirstChild(item.Name)
		if oldButton then
			oldButton:Destroy()
		end
		
		local newButton = itemFrame.TemplateButton:Clone()
		newButton.Name = item.Name
		newButton.TowerName.Text = item.Name
		newButton.Image = item.Config.Image.Texture
		newButton.LayoutOrder = item.Config.Price.Value
		newButton.Parent = itemFrame
		newButton.Visible = true
		
		newButton.Activated:Connect(function()
			local coinLeaderstats = player:WaitForChild("leaderstats").Coins
			if coinLeaderstats.Value >= item.Config.Price.Value then
				--coinLeaderstats.Value -= item.Config.Price.Value
				
				local remoteItem = item.Config.Price
				ReplicatedStorage.Remotes.SaveData:FireServer(remoteItem)
				if item then
					local newitem = item:Clone()
					ReplicatedStorage.Remotes.ShopItemGiver:FireServer(player, newitem)
				else
					warn("erteyertert")
				end
				
				PurchaseSound:Play()
			else
				-- error
				warn("Not enough coins!")
				ErrorSound:Play()
			end
		end)
	end
end

local function toggleShop()
	shopUI.Container.Visible = not shopUI.Container.Visible
	if shopUI.Container.Visible then
		updateItems()
	end
end

local function setupShop()
	local prompt = Instance.new("ProximityPrompt")
	prompt.RequiresLineOfSight = true
	prompt.ActionText = "Shop"
	if workspace.Shop:WaitForChild("ShopPart") then
		prompt.Parent = workspace.Shop:FindFirstChild("ShopPart")
	end
	
	prompt.Triggered:Connect(toggleShop)
	exit.Activated:Connect(toggleShop)
end

setupShop()

Script

local replicatedStorage = game:GetService("ReplicatedStorage")
local event = replicatedStorage.Remotes.ShopItemGiver

event.OnServerEvent:Connect(function(player, newitem)
	if newitem and newitem ~= nil then
		local Backpack = player:WaitForChild("Backpack", 30)
		print(newitem)
		newitem.Parent = Backpack
	else
		warn("No Item")
	end
end)
1 Like

Its because you’re trying to parent the player to their own backpack.
This is happening because when you are firing the remote to the server, you are passing the player as an argument

when firing remotes to the server the player is automatically passed as the first argument so there is no need for you to pass the player. Instead, change that line to this:

ReplicatedStorage.Remotes.ShopItemGiver:FireServer(newitem) -- No player
1 Like

You solved the error, but now on the server script it warns me about having “No Item”

Item is nil but it shouldnt be?

That is because you cloned the item locally, and since it is on the players local machine the server can not see it. Instead pass the item you want to be cloned and then clone it on the server.

Hey there. sorry for not replying. I cloned it on the server and it all works now! thanks alot

Server Code:

local replicatedStorage = game:GetService("ReplicatedStorage")
local event = replicatedStorage.Remotes.ShopItemGiver

event.OnServerEvent:Connect(function(player, item)
	if item and item ~= nil then
		local Backpack = player:WaitForChild("Backpack", 30)
		print(item)
		local newItem = item:Clone()
		newItem.Parent = Backpack
	else
		warn("No Item")
	end
end)

client code:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local Items = ReplicatedStorage:WaitForChild("Gears"):GetChildren()

local Container = script.Parent.Container
local itemFrame = Container.ItemsFrame

local shopUI = script.Parent
local exit = shopUI.Container.Exit

local player = Players.LocalPlayer

local PurchaseSound = shopUI.Sounds.Purchase
local ErrorSound = shopUI.Sounds.Error

function updateItems()
	for i, item in pairs(Items) do
		-- Find any old buttons
		local oldButton = itemFrame:FindFirstChild(item.Name)
		if oldButton then
			oldButton:Destroy()
		end
		
		local newButton = itemFrame.TemplateButton:Clone()
		newButton.Name = item.Name
		newButton.TowerName.Text = item.Name
		newButton.Image = item.Config.Image.Texture
		newButton.LayoutOrder = item.Config.Price.Value
		newButton.Parent = itemFrame
		newButton.Visible = true
		
		newButton.Activated:Connect(function()
			local coinLeaderstats = player:WaitForChild("leaderstats").Coins
			if coinLeaderstats.Value >= item.Config.Price.Value then
				--coinLeaderstats.Value -= item.Config.Price.Value
				
				local remoteItem = item.Config.Price
				ReplicatedStorage.Remotes.SaveData:FireServer(remoteItem)
				if item then
					ReplicatedStorage.Remotes.ShopItemGiver:FireServer(item)
				else
					warn("erteyertert")
				end
				
				PurchaseSound:Play()
			else
				-- error
				warn("Not enough coins!")
				ErrorSound:Play()
			end
		end)
	end
end

local function toggleShop()
	shopUI.Container.Visible = not shopUI.Container.Visible
	if shopUI.Container.Visible then
		updateItems()
	end
end

local function setupShop()
	local prompt = Instance.new("ProximityPrompt")
	prompt.RequiresLineOfSight = true
	prompt.ActionText = "Shop"
	if workspace.Shop:WaitForChild("ShopPart") then
		prompt.Parent = workspace.Shop:FindFirstChild("ShopPart")
	end
	
	prompt.Triggered:Connect(toggleShop)
	exit.Activated:Connect(toggleShop)
end

setupShop()

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.