Why is this Server > Client communication not working?

I’m making a trading system and I want to make the local script of the person putting the item in the trade to tell the server, which will fire the client of the person receiving the item. The client > server communication is fine, but the server > client communication is not working. Here is the local script for the person putting in the item:

	clone.Button.MouseButton1Click:Connect(function()
		if #script.Parent.Parent.GivingItems:GetChildren() < 10 then
		local givetemp = clone:Clone()
		local giveac = child:Clone()
		giveac.Parent = script.Parent.Parent.GivingItems
		givetemp.Parent = script.Parent.Parent.Parent.TradeFrame.TradeFrame.GiveFrame
			clone.Visible = false
			script.Parent.Parent.GiveItemAdded:FireServer(giveac,tradeclient)
		givetemp.Button.MouseButton1Click:Connect(function()
			clone.Visible = true
			givetemp:Destroy()
				
				script.Parent.Parent.GiveItemRemoved:FireServer(giveac,tradeclient)
				giveac:Destroy()
			end)
			end
		end)

Here is the server script:

script.Parent.GiveItemAdded.OnServerEvent:Connect(function(oplr,accessory,tradeclient)
	script.Parent.GiveItemAdded:FireClient(tradeclient,accessory)
	print("picked up on server")
	print(tradeclient.Name)
end)

Here is the script for the client getting the item:

script.Parent.Parent.GiveItemAdded.OnClientEvent:Connect(function(accessory)
	print(accessory.Name)
	local aclone = accessory:Clone()
	local tempclone = script.ItemTemplate:Clone()

	aclone.Parent = tempclone.ItemImage
	local cam = Instance.new("Camera")
	local handle = aclone.Handle
	cam.CFrame = CFrame.lookAt(Vector3.new(handle.Position.X+handle.Size.X,handle.Position.Y+handle.Size.Y,handle.Position.Z+handle.Size.Z),handle.Position)
	tempclone.CurrentCamera = cam
	tempclone.Parent = script.Parent.Parent.TradeFrame.GetFrame
	local gaclone= accessory:Clone()
	gaclone.Parent = script.Parent.Parent.GettingItems
end)

Thanks for reading! :happy3:

2 Likes

My only assumption is that multiple users have different RemoteEvents? You should just put them inside ReplicatedStorage rather than just parenting them in the GUI itself

This solved the problem, but for some reason the accessory doesn’t exist for the client getting the item

1 Like

Really? Does the print() statement give you a nil value or something?

Are there any other errors on your Output?

It doesn’t print nil, instead it errors: Players.COOLGUY16T.PlayerGui.ShopUI.TradeFrame.YourItems.InventoryScript:59: attempt to index nil with ‘Name’

1 Like

Yeah that explains it

This happens after the OnServerEvent function is called, right?

Just try printing accessory instead of the name on all of your scripts, and see if that changes anything?

1 Like

When I do that, it prints nil.

Yes

It’s probably having something to do relevant with your giveac variable & your passed accessory variable then, way back in your script where the player first sends the item, print what giveac is actually supposed to be?

giveac is a clone of child. child is the arguement (or parameter I dont know the right word) for the ChildAdded event of the player’s inventory

You still could be passing a nil value from the start though

Just to confirm, try this on the server side?

script.Parent.GiveItemAdded.OnServerEvent:Connect(function(oplr,accessory,tradeclient)
    print(accessory)
    print(tradeclient)

	script.Parent.GiveItemAdded:FireClient(tradeclient,accessory)
	print("picked up on server")
	print(tradeclient.Name)
end)

When I print accessory it prints nil

Ok the issue is within your FIRST SCRIPT THEN

	clone.Button.MouseButton1Click:Connect(function()
		if #script.Parent.Parent.GivingItems:GetChildren() < 10 then
		local givetemp = clone:Clone()
		local giveac = child:Clone()
		giveac.Parent = script.Parent.Parent.GivingItems
		givetemp.Parent = script.Parent.Parent.Parent.TradeFrame.TradeFrame.GiveFrame
			clone.Visible = false
             print(giveac)
			script.Parent.Parent.GiveItemAdded:FireServer(giveac,tradeclient)
		givetemp.Button.MouseButton1Click:Connect(function()
			clone.Visible = true
			givetemp:Destroy()
				             print(giveac)
				script.Parent.Parent.GiveItemRemoved:FireServer(giveac,tradeclient)
				giveac:Destroy()
			end)
			end
		end)

We’ve dumbed it down to here

The accessory is still equal to nil?

This is probably the most closest thing I could find, you referenced this earlier ago & this could be the instance as to why it keeping printing out nil

Double check your code & make sure that giveac is a valid variable?

I dont know what makes a variable valid but it’s in the script so does that count?

local Nonvalid = nil
local Nonvalid2
--These would be considered non-valid, since they're equal to nothing
local Valid = 50
local Valid2 = true
--These however have valid, since they have a value inside them that define the variable

Just check your ChildAdded event and see what you’re adding is correct

Is this correct?

inventory.ChildAdded:Connect(function(child)
	local clone = script.ItemTemplate:Clone()
	local aclone = child:Clone()
	clone.ItemName.Text = child.Name
	aclone.Parent = clone.ItemImage
	local cam = Instance.new("Camera")
	local handle = aclone.Handle
	cam.CFrame = CFrame.lookAt(Vector3.new(handle.Position.X+handle.Size.X,handle.Position.Y+handle.Size.Y,handle.Position.Z+handle.Size.Z),handle.Position)
	clone.ItemImage.CurrentCamera = cam
	clone.Parent = script.Parent
	clone.Button.MouseButton1Click:Connect(function()
		if #script.Parent.Parent.GivingItems:GetChildren() < 10 then
			local givetemp = clone:Clone()
			local giveac = child:Clone()
			giveac.Parent = script.Parent.Parent.GivingItems
			givetemp.Parent = script.Parent.Parent.Parent.TradeFrame.TradeFrame.GiveFrame
			clone.Visible = false
			print(giveac)
			game.ReplicatedStorage.GiveItemAdded:FireServer(giveac,tradeclient)
			givetemp.Button.MouseButton1Click:Connect(function()
				clone.Visible = true
				givetemp:Destroy()
				print(giveac)
				game.ReplicatedStorage.GiveItemRemoved:FireServer(giveac,tradeclient)
				giveac:Destroy()
			end)
		end
	end)
	
end)
1 Like
inventory.ChildAdded:Connect(function(child)
    print(child)
	local clone = script.ItemTemplate:Clone()
	local aclone = child:Clone()
	clone.ItemName.Text = child.Name
    print(aclone)
	aclone.Parent = clone.ItemImage
	local cam = Instance.new("Camera")
	local handle = aclone.Handle
	cam.CFrame = CFrame.lookAt(Vector3.new(handle.Position.X+handle.Size.X,handle.Position.Y+handle.Size.Y,handle.Position.Z+handle.Size.Z),handle.Position)
	clone.ItemImage.CurrentCamera = cam
	clone.Parent = script.Parent
	clone.Button.MouseButton1Click:Connect(function()
		if #script.Parent.Parent.GivingItems:GetChildren() < 10 then
			local givetemp = clone:Clone()
			local giveac = child:Clone()
			giveac.Parent = script.Parent.Parent.GivingItems
			givetemp.Parent = script.Parent.Parent.Parent.TradeFrame.TradeFrame.GiveFrame
			clone.Visible = false
			print(giveac)
			game.ReplicatedStorage.GiveItemAdded:FireServer(giveac,tradeclient)
			givetemp.Button.MouseButton1Click:Connect(function()
				clone.Visible = true
				givetemp:Destroy()
				print(giveac)
				game.ReplicatedStorage.GiveItemRemoved:FireServer(giveac,tradeclient)
				giveac:Destroy()
			end)
		end
	end)
	
end)

When all else fails, darn debug this script

Sooooo close! The server thinks it isn’t nil but the client getting the item still thinks it does

Ok could you send me a place file then? I might know where the instance could lie, but I’m not exactly certain

THIS PASSING NIL STUFF IS A HUGE BRUH MOMENT