Help with a function that returns nil

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!

The function runs the first time I use it in game but fails the second time
Its suppost to update the selected item from the shop and it works im able to sell off the first item I pick but when i go to pick a second one i get:

Shop Handler:170: attempt to call a nil value

on the actual function call – selectedItem(itemButton) –

How can it return nil if it works once? doesnt that mean that the function is present? do i have to reset it somehow?

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

function work the first time it is used but returns nil after

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

i tried fiddleing with names and stuff but this has worked before and just stopped all of a sudden without me changing the code around it

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local function selectedItem(itemButton)
		local itemFrame = itemButton.Parent
		selectedItem = itemFrame.Name -- ITEM SELECTED
		sellNameText.Text = selectedItem
		localAmount = 0
		sellAmount.Text = localAmount
		print("selected: " .. selectedItem)
		totalowned.Text = "you have "..plrInv[selectedItem]

		slectedItemFrame.CurrentCamera = nil  -- SELECTED ITEM VIEWPORT UPDATE
		debris:AddItem(slectedItemFrame:FindFirstChildOfClass("Model"), 0)
		debris:AddItem(slectedItemFrame:FindFirstChildOfClass("Camera"), 0)
		local cloneItemFrameImage = itemFrame.itemImageFrame:FindFirstChildOfClass("Model"):Clone()
		cloneItemFrameImage.Parent = slectedItemFrame
		local cloneItemFrameCamera = itemFrame.itemImageFrame:FindFirstChildOfClass("Camera"):Clone()
		cloneItemFrameCamera.Parent = slectedItemFrame
		slectedItemFrame.CurrentCamera = slectedItemFrame.Camera ----- :)
	end

	for _, itemFrame in ipairs(shopButton) do  -- CHECK THE CLICK FOR ALL BUTTONS
		if itemFrame.shopItem:IsA("TextButton")	then
			local itemButton = itemFrame.shopItem
			local itemImageFrame = itemFrame.itemImageFrame
			local itemModel = itemImageFrame:FindFirstChildOfClass("Model")
			--print(itemButton)

			itemButton.MouseButton1Click:Connect(function() -- CLICK ON SHOP BUTTON

				selectedItem(itemButton)
			end)


		end

	end

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

1 Like

It looks like you have a function and a variable both named selectedItem? Not saying this is the issue but I would maybe capitalize the function SelectedItem. And can you show a pic of the whole error.

it might be working now… how silly

1 Like

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