How Do I Fix attempt to index nil 'Clone'

I was following a tutorial on grid placements and when i test it,
it doesnt seem to work, i check on the dev console and find theres an error on line 100
stating index nil with ‘Clone’

  1. What is the issue?
    with the error i couldnt continue, i searched the script and couldnt find a thing to fix

  2. What solutions have you tried so far?

i have look up on the devforum but its still confusing but i know what attempt with index nil means but idk where did i get wrong

Heres The Code

-- Settings

--Booleans
local interpolation = true
local moveByGrid = true
local buildPlacement = true

-- Ints
local rotStep = 90
local maxHeight = 90

-- Numbers/Floats
local lerpSpeed = 0.8

-- Other
local gridTexture = ""

local placement = {}
placement.__index = placement

local players = game:GetService("Players")
local runService = game:GetService("RunService")

local player = players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local mouse = player:GetMouse()
-- Constructure Var
local GRID_SIZE
local ITEM_LOC
local ROT_KEY
local TERM_KEY

-- Activation Var
local object
local placedObject
local plot
local stackable

-- Calc Var
local posX
local posY
local posZ

-- calculatate inital y pos
local function calculateYpos()
	
end

local function snap(x)
	return math.round(x/GRID_SIZE) * GRID_SIZE
end

--calculate the model pos on grid
local function calculateItemPos()
	if moveByGrid then
		posX = snap(mouse.Hit.X)
		posY = 4
		posZ = snap(mouse.Hit.Z)
	else
		posX = mouse.Hit.X
		posY = mouse.Hit.Y
		posZ = mouse.Hit.Z
	end
end

--sets model pos on pivot
local function translateObj()
	if placedObject and object.Parent == placedObject then
		calculateItemPos()
		
		object:PivotTo(CFrame.new(posX, posY, posZ))
	end
end

local function approvePlacement()
	return true
end

-- Constructor Function
function placement.new(g, obj, r, t)
	local data = {}
	local metaData =  setmetatable(data, placement)
	
	GRID_SIZE = g
	ITEM_LOC = obj
	ROT_KEY = r
	TERM_KEY = t
	
	data.grid = GRID_SIZE
	data.itemLocation = ITEM_LOC
	data.rotateKey = ROT_KEY
	data.terminateKey = TERM_KEY
	
	return data
end

-- Activate Placement Id = Name, pobjs = placeObjects, plt = plot, stk = stackable
function placement:activate(id, pobjs, plt, stk)
	--assigning values
	object = ITEM_LOC:FindFirstChild(id):Clone()
	placedObject = pobjs
	plot = plt
	stackable = stk
	
	--make sure placement can activate properly
	if not approvePlacement() then
		return "Placement Could not Activate"
	end
	
	-- filters mouse object on stackable var
	if not stackable then
		mouse.TargetFilter = placedObject
	else
		mouse.TargetFilter = object
	end
	
	object.Parent = placedObject	
end

runService:BindToRenderStep("Input", Enum.RenderPriority.Input.Value, translateObj)

return placement

the error is exactly twice under function placement:activate(id, pobjs, plt, stk)
or under the –assigning values comment

1 Like

The method :WaitForChild has two possible outcomes: the instance you are looking for, or nil when the instance cannot be found. Because you are trying to call :Clone() directly on the result, there’s the possibility that it will try to run a method on nil, which is what’s happening. You need some kind of check to make sure whatever :FindFirstChild is looking for actually exists first.

1 Like

When it says attempt to index nil it means you tried to access a property/method of a nil value
In this line

object = ITEM_LOC:FindFirstChild(id):Clone()

When it says attempt to index nil with Clone it means that
ITEM_LOC:FindFirstChild(id) was nil meaning it failed to find that child
the engine will see (nil):Clone()
Your trying to index a nil value with Clone

1 Like

this may be happening because the object doesnt exist. Maybe try this:

	--assigning values
	local FoundObject = ITEM_LOC:FindFirstChild(id)
	if not FoundObject then warn("Could not find object") return end
	object = FoundObject:Clone()
	placedObject = pobjs
	plot = plt
	stackable = stk

will output “Could not find object” if the object could not be found.

1 Like

Make sure that you are calling module:active(a, b, c, d) in your local script and not module.activate(a, b, c, d), as when you use a period, it thinks the first argument is referring to self.

Suppose you print all the parameters within your function:
a = b
b = c
c = d
d = nil

2 Likes

ok im going to try it see if it works

ok yeah i understand im going to recheck on the tutorial

its actually a module script btw

heres the local script:

  • Turns out i misspelled the item word
  • I Also forgot to set the primary part
  • And Heres The Script
local module = require(game.ReplicatedStorage.Modules.placement)

local placement = module.new(
	2,
	game.ReplicatedStorage.Items,
	Enum.KeyCode.R, Enum.KeyCode.X
)

wait(2)

placement:activate("Rail", workspace.Floor.ItemHolder, workspace.Floor, false)

For anybody whos got “index nil” error on the grid placement tutorial system.

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