Why can't I properly access this table?

I want to print the items in a player’s inventory when you touch a cube. I get this error message when I touch the cube: Stick is not a valid member of Player “Players.MrMatthewGamingX”

Server side script

script.Parent.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent) --// a player service function to get the player from their character
	if player and hit.Parent:FindFirstChild("Humanoid") then
		game.ReplicatedStorage.PrintInventory:FireClient(player)
		
	end
end)

game.ReplicatedStorage.PrintInventory.OnServerEvent:Connect(function(Items)
	if Items["Stick"] > 0 then
		warn("Sticks ("..Items[1]..")")
	end
	if Items["Hide"] > 0 then
		warn("Hides ("..Items[2]..")")
	end
	if Items["Stick"] == 0 and Items["Hide"] == 0 then
		warn("Inventory is empty")
	end


end)

Inventory script in StarterPlayerScripts

local Items = {
	Stick = 0,
	Hide = 0
}

local function HideCollect()
	Items.Hide += 1
	print("Hide + 1")
end

local function StickCollect()
	Items.Stick += 1
	print("Stick + 1")
end

game.ReplicatedStorage.HideCollect.OnClientEvent:Connect(HideCollect)
game.ReplicatedStorage.StickCollect.OnClientEvent:Connect(StickCollect)

game.ReplicatedStorage.PrintInventory.OnClientEvent:Connect(function()
	game.ReplicatedStorage.PrintInventory:FireServer(Items)
end)

When you fire an event player is assumed to be the first parameter when you receive the event. Try this line instead:

game.ReplicatedStorage.PrintInventory.OnServerEvent:Connect(function(player, Items)

Thank you very much. I didn’t know that was how things worked!

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