When i send a table to the server the server sees it as nil

for some reason when i send a table to a server the server sees it as nil even though there is elements inside of it (instances) a gui frame

heres the local script

local Products = {}

script.Parent.ChildAdded:Connect(function(child)
	
	wait(0.1)
	
	table.insert(Products, child)
	
end)

script.Parent.ChildRemoved:Connect(function(child)

	wait(0.1)

	for i,v in pairs(Products) do
		
		if v.Name == child.Name then
			
				table.remove(Products, i)
			
			print(Products)
			
				return 
		end
		
	end
	

end)

script.Parent.Parent.Parent.PurchaseButton.MouseButton1Click:Connect(function()
	
	print(Products)
	
	game.ReplicatedStorage.Event.SendToServerProduct:FireServer(Products)

end)

and heres the server script

game.ReplicatedStorage.Event.SendToServerProduct.OnServerEvent:Connect(function(plr, product)
	
print(product)	

end)
2 Likes

Edit : it dosent see it as nil rather it sees the table empty

1 Like
  1. Are the instances created on the client? If so, the server cannot see them.
  2. You are only adding to the table on ChildAdded, but the children may already be there. You should loop through the current children and add them to the products table.
  3. Use task.wait rather than wait.
1 Like

the instances are being created in the client because they’re gui elements. i cant see how i can create gui elements in the server.

1 Like

Instances created in a LocalScript will be created on the client and not replicated to the server for security. To create them on the server, create them in a Script (server script) instead.

2 Likes

i just fixed it , instead of putting the instance in the table i will just add the name which will be a string for anyone who is curious on the solution here it is

local script

local Products = {}

script.Parent.ChildAdded:Connect(function(child)
	
	wait(0.1)
	
	table.insert(Products, child.Name)
	
end)

script.Parent.ChildRemoved:Connect(function(child)

	wait(0.1)

	for i,v in pairs(Products) do
		
		if v.Name == child.Name then
			
				table.remove(Products, i)
			
			print(Products)
			
				return 
		end
		
	end
	

end)

script.Parent.Parent.Parent.PurchaseButton.MouseButton1Click:Connect(function()
	
	print(Products)
	
	game.ReplicatedStorage.ZeamzorEvents.SendToServerProduct:FireServer(Products)

end)
1 Like

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