Attempt to call a nil value when using :GetChildren()

What I’m trying to achieve

I’m trying to make a vending machine which functions when the code is correct it gives an item, however, when it goes to get the children and clone the item it gives an error. Any help is appreciated!

My code:

local Tools = script.Parent.Tools
local Selections = script.Parent.Selections
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")
local enter = script.Parent.Screen.SurfaceGui.Select.Enter
local cancel = script.Parent.Screen.SurfaceGui.Select.Cancel

local UIinfo = TweenInfo.new(
	0.1, --Time
	Enum.EasingStyle.Sine, -- Style of door open animation
	Enum.EasingDirection.InOut, -- Direction of easing.
	0, -- Repeat count.
	false, -- Will it reverse?
	0 -- The delay time.
)


if game.IsLoaded then
	Tools.Parent = ReplicatedStorage
end


local result = ""

for i, v in pairs(script.Parent.Screen.SurfaceGui.Select:GetChildren()) do
	if not v:IsA("TextButton") then continue end 
	local uithickness = v.UIStroke
	local tweenenter = TweenService:Create(uithickness, UIinfo, {Thickness = 4})
	local tweenleave = TweenService:Create(uithickness, UIinfo, {Thickness = 2})
	v.MouseEnter:Connect(function()
		tweenleave:Cancel()
		tweenenter:Play()
	end)
	v.MouseLeave:Connect(function()
		tweenenter:Cancel()
		tweenleave:Play()
	end) 
	v.MouseButton1Down:Connect(function()
		if v.Name == "Enter" then 
			print(result) 
			if Selections:FindFirstChild(result) then
				print("Found".. result)
				-- Clones the item
				
				
				
				
				
				-- Where I'm trying to clone
				local item = Selections:FindFirstChild(result).Item:GetChildren()
				item:Clone()
				item.Position = result.Dropper
				
				
				
				
				
				
				
					elseif not Selections:FindFirstChild(result) then
				print("Not found!")
				script.Parent.Screen.SurfaceGui.Text.SelectionNo.Text = "Item does not exist."
				wait(2)
				script.Parent.Screen.SurfaceGui.Text.SelectionNo.Text = result
					end
		
		elseif v.Name == "Cancel" then 
			result = ""
			script.Parent.Screen.SurfaceGui.Text.SelectionNo.Text = "__"
		else
			result ..= v.Text
			
			script.Parent.Screen.SurfaceGui.Text.SelectionNo.Text = result
		end
	end)
end

You are trying to clone a table. Instead, loop through the table for the items and clone.

local items = Selections:FindFirstChild(result).Item:GetChildren()
for i, item in pairs(items) do
	item:Clone()
	item.Position = result.Dropper
end
1 Like

Hia, I get this error for the position piece of the code:

Workspace.Vending.Main_Code:53: invalid argument #3 (Vector3 expected, got nil)

Also that has fixed my :Clone() bit so thank you for the information!

Ah, my bad. Switch result.Dropper for result.Dropper.Position.

local items = Selections:FindFirstChild(result).Item:GetChildren()
for i, item in pairs(items) do
	item:Clone()
	item.Position = result.Dropper.Position
end
1 Like

I now get this: Workspace.Vending.Main_Code:53: attempt to index nil with ‘Position’

Also, ty for the help!

It seems that result is a string, causing it to error when trying to use .Dropper.Position on it. Where exactly are you trying to get the item to spawn?

In front of the coke in the vending machine and then I want it to unanchor, its in a folder in workspace.

image

Try this:

local selected = Selections:FindFirstChild(result)
local items = selected.Item:GetChildren() 
for _, item in pairs(items) do 
	local clone = item:Clone()
	clone.Position = selected.Dropper.Position
end 
1 Like

Hm still gives a position error.

Hmmm it didn’t give an error, it just did nothing.