Attempt to index nil with 'Clone'?

What my script does is when a UI button is clicked the game functions a function called CloneService with the name of the object. When I click the UI the first time it works but if I click the same UI with the same object name it gives me the problem in the output. If I press different object UIs and not the same twice it works.

if you can please help here is my script:

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local Camera = game.Workspace.CurrentCamera

local BuildPro = false
local OnPlot = false

local makeSureObjIsInGrid = require(game.ReplicatedStorage.BuildScriptModule)

local BuildUi = script.Parent.FHOS.Frame.ObjectSelection

local MarketPlaceService = game:GetService("MarketplaceService")
local RunService = game:GetService("RunService")
game.Players.PlayerAdded:Connect(function()
	local hasPass = false
	
	local success, message = pcall(function()
		hasPass = MarketPlaceService:UserOwnsGamePassAsync(Player.UserId, 11321181)
	end)
	if not success then
		warn("Error while checking if player has pass: " .. tostring(message))
		return
	end
	if hasPass == true then
		BuildPro = true
	end
end)

local Grid = 2

script.Parent.Grid:GetPropertyChangedSignal("Value"):Connect(function()
	Grid = script.Parent.Grid.Value
end)

local PosX
local PosY
local PosZ
local Plot
local plotName
local Object

local GivenPlot = game.ReplicatedStorage.BuildEvents.GivenPlot
local PlaceObject = game.ReplicatedStorage.BuildEvents.PlaceObject
GivenPlot.OnClientEvent:Connect(function(plt)
	Plot = plt
	plotName = plt.Name
end)

local Rot = Plot.Orientation.Y

Plot.Touched:Connect(function(hit)
	local plr = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
	if plr then
		OnPlot = true
	end
end)
Plot.TouchEnded:Connect(function(hit)
	local plr = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
end)
local CanStart = true
local Placing = nil
local CanPlace = nil
local MoveObject = false
local CanPlace
local BuildMode = Instance.new("BoolValue")
BuildMode.Parent = Player
BuildMode = false
BuildSelection = BuildUi.ScrollingFrame
Soil = BuildSelection.SoilSelect.ImageButton
HayBale = BuildSelection.HayBaleSelect.ImageButton
Log = BuildSelection.LogSelect.ImageButton
GreenScreen = BuildSelection.GreenScreenSelect.ImageButton
Flag = BuildSelection.FlagSelect.ImageButton
SmallDeck = BuildSelection.SmallDeck.ImageButton
DeckStairs = BuildSelection.DeckStairs.ImageButton
LargeDeck = BuildSelection.LargeDeck.ImageButton
Chair = BuildSelection.Chair.ImageButton
Bed = BuildSelection.Bed.ImageButton
Table = BuildSelection.Table.ImageButton
CampFire = BuildSelection.CampFire.ImageButton
Wall = BuildSelection.Wall.ImageButton

script.Parent.FHOS.Frame.Frame.BuildModeApp.TextButton.MouseButton1Click:Connect(function()
	script.Parent.FHOS.Frame.Frame.Visible = false
	script.Parent.FHOS.Frame.FhBrand.Visible = false
	script.Parent.FHOS.Frame.Power.Visible = false
	script.Parent.FHOS.Frame.Time.Visible = false
	script.Parent.FHOS.Frame.Network.Visible = false
	script.Parent.FHOS.Frame.Battery.Visible = false
	script.Parent.FHOS:TweenPosition(UDim2.new(0.105, 0, 0.39, 0), "Out", "Linear", 1, false, nil)
	while script.Parent.FHOS.Rotation > -90 do
		script.Parent.FHOS.Rotation = script.Parent.FHOS.Rotation - 2
		wait()
	end
	BuildMode = true
	if BuildMode == true and OnPlot == true or BuildPro == true then
		Camera.CameraSubject = Plot
	end
end)

local function Round(n, to)
	local to = to or 1
	if to == 0 then return n end
	return math.floor(n/to + 0.5) * to
end

local function SnapToGrid(vector3)
	return Vector3.new(
		Round(vector3.X, Grid),
		Round(vector3.Y, 0),
		Round(vector3.Z, Grid)
	)
end

local function CloneService(Obj)
	local ObjN = Obj
	Object = nil
	if CanStart == true and not CanPlace and not Placing then
		Object = game.ReplicatedStorage.BuildObjects:FindFirstChild(ObjN):Clone()
		Object.Parent = game.Workspace
		Mouse.TargetFilter = Object
		
		Object.PrimaryPart.Orientation = Vector3.new(0, Object.PrimaryPart.Orientation.Y, 0)
		
		CanStart = false
		CanPlace = true
		Placing = true
	end
end

Soil.MouseButton1Click:Connect(function()
	CloneService("Soil")
	MoveObject = true
end)
HayBale.MouseButton1Click:Connect(function()
	CloneService("Haybale")
	MoveObject = true
end)
Log.MouseButton1Click:Connect(function()
	CloneService("LogSeat")
	MoveObject = true
end)
GreenScreen.MouseButton1Click:Connect(function()
	CloneService("GreenScreen")
	MoveObject = true
end)
Flag.MouseButton1Click:Connect(function()
	CloneService("Flag")
	MoveObject = true
end)
SmallDeck.MouseButton1Click:Connect(function()
	CloneService("SmallDeck")
	MoveObject = true
end)
DeckStairs.MouseButton1Click:Connect(function()
	CloneService("DeckStairs")
	MoveObject = true
end)
LargeDeck.MouseButton1Click:Connect(function()
	CloneService("LargeDeck")
	MoveObject = true
end)
Chair.MouseButton1Click:Connect(function()
	CloneService("Chair")
	MoveObject = true
end)
Bed.MouseButton1Click:Connect(function()
	CloneService("Bed")
	MoveObject = true
end)
Table.MouseButton1Click:Connect(function()
	CloneService("Table")
	MoveObject = true
end)
CampFire.MouseButton1Click:Connect(function()
	CloneService("Campfire")
	MoveObject = true
end)
Wall.MouseButton1Click:Connect(function()
	CloneService("Wall")
	MoveObject = true
end)

local function Place()
	if Placing and CanPlace then
		PosX = Object.PrimaryPart.Position.X
		PosY = Object.PrimaryPart.Position.Y
		PosZ = Object.PrimaryPart.Position.Z
		PlaceObject:FireServer(Object.Name, PosX, PosY, PosZ, Rot, Plot)
		Placing = false
		CanPlace = false
		CanStart = true
		
		Object:Destroy()
	end
end

local function GetMousePlacementCFrame(Plot, Object)
	local objectOffset = Vector3.new(0, Object.PrimaryPart.Size.Y/2, 0)
	
	local mouseHit = Mouse.Hit
	
	local cfToCheck = mouseHit+objectOffset
	local plotBottomCenter = Plot.CFrame*CFrame.new(0, Plot.Size.Y/2, 0)
	
	return makeSureObjIsInGrid(Object, cfToCheck, plotBottomCenter, Plot.Size.X, Plot.Size.Z, Grid)
end

local function updateModelPlacement()
	if Plot and (not Plot:IsDescendantOf(Object)) and Mouse.Target.Name == plotName then
		Object:SetPrimaryPartCFrame(GetMousePlacementCFrame(Plot, Object)*CFrame.Angles(0,math.rad(Rot),0))
		Object.PrimaryPart.Orientation = Vector3.new(0, Rot, 0)
	end
end
--TAKE THIS PART OUT FOR RELEASE
BuildMode = true

game:GetService("UserInputService").InputBegan:Connect(function(input, GPE)
	if input.KeyCode == Enum.KeyCode.R and Placing == true and CanPlace == true then
		Rot = Rot + 45
		Object:SetPrimaryPartCFrame(Object.PrimaryPart.CFrame * CFrame.Angles(0, math.rad(Rot), 0))
	end
end)

RunService.RenderStepped:Connect(function()
	Mouse.TargetFilter = Object
	if Placing == true and CanPlace == true and not CanStart then
		Plot.Texture.Transparency = 0
		updateModelPlacement()
	end
end)

Mouse.Button1Down:Connect(Place)

local autoSaveInterval = 15

while true do
	wait(autoSaveInterval)
	game.ReplicatedStorage.BuildEvents.Save:FireServer()
end

Here’s the main part where the issue is:

local function CloneService(Obj)
	if CanStart == true and not CanPlace and not Placing then
		Object = game.ReplicatedStorage.BuildObjects:FindFirstChild(Obj):Clone() --The problem
		Object.Parent = game.Workspace
		Mouse.TargetFilter = Object
		
		Object.PrimaryPart.Orientation = Vector3.new(0, Object.PrimaryPart.Orientation.Y, 0)
		
		CanStart = false
		CanPlace = true
		Placing = true
	end
end

Can you show where the error is the code is REALLY long

@Dolphin_Worm Here is the part of the script… I specifically added it in but that’s alright! :smiley:

Object = game.ReplicatedStorage.BuildObjects:FindFirstChild(Obj):Clone() --The problem

It’s giving you that error because BuildObjects:FindFirstChild(Obj) is nil or doesn’t exist. You can write a if statement to check if it’s there

if game.ReplicatedStorage.BuildObjects:FindFirstChild(Obj) then
Object =  game.ReplicatedStorage.BuildObjects:FindFirstChild(Obj):Clone()
else
print("This object doesn't exist")
end

It printed the object does not exist the second time… The first time it didn’t and let me place my Object… Could this be a roblox studio bug?

Just found out that when I clone the object is removed from RepStorage!!! How do I fix that???

Then check if there is any script that destroys or removes the object that is cloned. The item shouldn’t disappear if they are cloned.

Yes it does when you place. Is there a way to clone the object and know what the object’s clone is not the one from RepStorage?? It gets taken from repstorage and destroyed

You can have 2 variables. One variable for the cloned object and one for the oringinal object.