Why are arguments vanishing once passed via Remote Event?

local Products = require(workspace.Products)
local LootTables = {
	["Flour"] = {
		Item = "Flour",
		Amount = 1
	}
}
script.Parent.MouseButton1Click:Connect(function(player)
	local Ingredient = game.ReplicatedStorage.SelectedIngredient.Value
	local Product = Products[Ingredient]
	local LootTable = LootTables[Product]
	print(Ingredient)
	print(Product)
	print(LootTable)
	
	local Item = LootTable.Item
	local Number = LootTable.Amount
	--This section--
	game.ReplicatedStorage.Inventory.AddItem:FireServer(player, {
		Num1 = Item,
		Item1 = Number
	})
end)

This script will send out those 3 variables to the server. Using print statements in that script, I get “Flour” and “1”. However, when I use this script:

game.ReplicatedStorage.Inventory.AddItem.OnServerEvent:Connect(function(args)
	warn(args)
end)

All it does is print the name of the player. Why does this not work? I have other scripts like these:

game.ReplicatedStorage.Inventory.AddItem:FireClient(player, {
				Num1 = Num1,
				Item1 = Item1,
				Num2 = Num2,
				Item2 = Item2,
				Num3 = Num3,
				Item3 = Item3,

And

local function FillSlot(args)
	print(args)
	local Num1 = args.Num1
	local Num2 = args.Num2
	local Num3 = args.Num3
	local Num4 = args.Num4
--Rest of function not shown because it's not worth thit and is VERY long.

That work just fine. Why is this issue happening now?

OnServerEvent is a method of a remote event that connects a function to run when the event is fired by a client. The first parameter of the function is always the player object of the client that fired the event, and additional parameters are the arguments passed by the client. However, you are using args as the only parameter of the function, which means the script is expecting the client to pass a player object as an argument.
This is unnecessary and incorrect as the player object is automatically added by the engine. You should use two parameters instead, one for the player and one for the args, like this:

game.ReplicatedStorage.Inventory.AddItem.OnServerEvent:Connect(function(player, args) warn(args) end)

warn(args) ends up printing as nil.

Try changing this to this:

game.ReplicatedStorage.Inventory.AddItem:FireServer({ Num1 = Item, Item1 = Number })

Hope it helps I can’t really test the script myself at the moment.

It doesn’t come out as nil anymore. Yay! Would I be able to apply this same concept with BindableEvents instead and the FillSlot() function from the example earlier? That was my end goal (to have it be used in that function. The other function I’ve been using was for testing purposes.)

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