"Attempt to index nil with 'Clone'" error

Hey. I am having this issue.

In the script:
game.ReplicatedStorage.CreateTransaction.OnServerInvoke = function(player,item)
local coins = game.Players:FindFirstChild(player.Name).leaderstats.Coins
local cost = game.ServerStorage.ToolModels[item].Information.Cost

if coins.Value >= cost.Value and not game.ServerStorage.PlayerTools[player.Name]:FindFirstChild(item) then
	-- Player has enough money --
	coins.Value = coins.Value - cost.Value
	player.Character.Humanoid:UnequipTools()
	for i, tool in pairs(player.Backpack:GetChildren()) do
		if tool.Name == player.Equipped.Value then
			tool:Destroy()
			
		end
		
	end
	

	player.Equipped.Value = item
	
	local tool = game.ServerStorage.Tools:FindFirstChild(item):Clone()  //Line 70
	tool.Parent = game.ServerStorage.PlayerTools[player.Name]
	
	local tool = game.ServerStorage.Tools:FindFirstChild(item):Clone()
	tool.Parent = player.Backpack
	
	return true
	
	
elseif cost.Value > coins.Value and not game.ServerStorage.PlayerTools [player.Name]:FindFirstChild(item) then
	return "not enough coins"
else
	
	return "already bought"
end

end


it says this:
14:55:45.100 ServerScriptService.ForSpriteShop:70: attempt to index nil with ‘Clone’ - Client - LocalScript:6


This script is activated here:
script.Parent.MouseButton1Click:Connect(function()

local outcome = game.ReplicatedStorage.CreateTransaction:InvokeServer(modelName.Value)

if outcome == true then
	
	-- Purchase was successful
	
	script.Parent.Text = "Equipped"

elseif outcome == "not enough coins" then
	-- They do not have enough cash
elseif outcome == "already bought" then
	if game.Players.LocalPlayer.Backpack.Value ~= modelName.Value then
		game.ReplicatedStorage.EquipTool:FireServer(modelName.Value)
		script.Parent.Text = "Equipped"
	end
end

end)


Any support will be appreciated.

Which script is getting the error?

1 Like

The local script that has:
script.Parent.MouseButton1Click:Connect(function()

local outcome = game.ReplicatedStorage.CreateTransaction:InvokeServer(modelName.Value)

if outcome == true then
	
	-- Purchase was successful
	
	script.Parent.Text = "Equipped"

elseif outcome == "not enough coins" then
	-- They do not have enough cash
elseif outcome == "already bought" then
	if game.Players.LocalPlayer.Backpack.Value ~= modelName.Value then
		game.ReplicatedStorage.EquipTool:FireServer(modelName.Value)
		script.Parent.Text = "Equipped"
	end
end

end)

Assuming from the location of the error, it’s in some sort of a LocalScript

You can’t put LocalScripts in ServerScriptService, it won’t be able to work that way

Line 6 would be resultant to a blank line of code, with nothing in it

Referring back to the Server Script:

Just do player:WaitForChild("leaderstats").Coins…?

2 Likes

This means that the object that your trying to clone does not exist.

1 Like

Thanks for the effort, but that LocalScript is not in ServerScriptService, it is in StarterGUI. I also tried your idea for the server script, but that has the same results.

For more information, here is the local script:
local modelName = script.Parent.Parent.ModelName
local boxName = script.Parent.Parent.Parent.CurrentBox

script.Parent.MouseButton1Click:Connect(function()

local outcome = game.ReplicatedStorage.CreateTransaction:InvokeServer(modelName.Value)

if outcome == true then
	
	-- Purchase was successful
	
	script.Parent.Text = "Equipped"

elseif outcome == "not enough coins" then
	-- They do not have enough cash
elseif outcome == "already bought" then
	if game.Players.LocalPlayer.Backpack.Value ~= modelName.Value then
		game.ReplicatedStorage.EquipTool:FireServer(modelName.Value)
		script.Parent.Text = "Equipped"
	end
end

end)

boxName:GetPropertyChangedSignal(“Value”):Connect(function()

local model = nil

for i, object in pairs(game.Workspace.ItemRoller["Box"..boxName.Value]:GetChildren()) do

	if object:IsA("Model") then
		model = object -- Swtting the model variable to the object
	end

end

local toolsBought = game.ReplicatedStorage.GetToolsBought:InvokeServer()

for i, tool in pairs(toolsBought) do
	
	if tool == model.Name then
		-- Already bought by the player
		
		script.Parent.Text = "Equipped"
	else
		script.Parent.Text = "Equip"
		
	end
	
end

end)


and here is the Server Script:
– Generate Blocks –

local numberOfBlocks = #game:GetService(“ServerStorage”):WaitForChild(“ToolModels”):GetChildren()

local studGap = 9 – Gab between each box

local box = game.Workspace:WaitForChild(“ItemRoller”).Box0

for i = 1,numberOfBlocks-1,1 do
newbox = box:Clone()
newbox:SetPrimaryPartCFrame(newbox.PrimaryPart.CFrame * CFrame.new(-16*i,0,0))
newbox.Name = “Box”…i
newbox.Parent = game.Workspace.ItemRoller
end

for i, v in pairs(game.ServerStorage.ToolModels:GetChildren()) do
if v then
new = v:Clone()
new:SetPrimaryPartCFrame(game.Workspace.ItemRoller[“Box”…i-1].Hitbox.CFrame)
new.Parent = game.Workspace.ItemRoller[“Box”…i-1]
end

end

game.ReplicatedStorage:WaitForChild(“RequestInformation”).OnServerInvoke = function(plr,item)

local data = {}
local bought = false
local description = game.ServerStorage.ToolModels[item.Name].Information.Description.Value
local title = item.Name
local cost = game.ServerStorage.ToolModels[item.Name].Information.Cost.Value
local costType = game.ServerStorage.ToolModels[item.Name].Information.CostType.Value
if game.ServerStorage.PlayerTools[plr.Name]:FindFirstChild(item.Name) then 
	-- They have bought it
	bought = true
else
	bought = false
end

table.insert(data,1,title)
table.insert(data,2,description)
table.insert(data,3,cost)
table.insert(data,4,costType)
table.insert(data,5,bought)

return data

end

game.ReplicatedStorage.CreateTransaction.OnServerInvoke = function(player,item)
local coins = player:WaitForChild(“leaderstats”).Coins
local cost = game.ServerStorage.ToolModels[item].Information.Cost

if coins.Value >= cost.Value and not game.ServerStorage.PlayerTools[player.Name]:FindFirstChild(item) then
	-- Player has enough money --
	coins.Value = coins.Value - cost.Value
	player.Character.Humanoid:UnequipTools()
	for i, tool in pairs(player.Backpack:GetChildren()) do
		if tool.Name == player.Equipped.Value then
			tool:Destroy()
			
		end
		
	end
	

	player.Equipped.Value = item
	
	local tool = game.ServerStorage.Tools:FindFirstChild(item):Clone()
	tool.Parent = game.ServerStorage.PlayerTools[player.Name]
	
	local tool = game.ServerStorage.Tools:FindFirstChild(item):Clone()
	tool.Parent = player.Backpack
	
	return true
	
	
elseif cost.Value > coins.Value and not game.ServerStorage.PlayerTools [player.Name]:FindFirstChild(item) then
	return "not enough coins"
else
	
	return "already bought"
end

end

game.ReplicatedStorage.GetToolsBought.OnServerInvoke = function(player)
local tools = {}

for i, tool in pairs(game.ServerStorage.PlayerTools[player.Name]:GetChildren()) do 
	table.insert(tools,tool.Name)
end	

return tools

end

I don’t understand how to fix this issue. Here is a pic of the location of the object I am trying to clone:

vbcvb

You could add some checks here to make sure that the object exists first

local tool = game.ServerStorage.Tools:FindFirstChild(item)

if tool then
    tool = tool:Clone()
    tool.Parent = game.ServerStorage.PlayerTools[player.Name]

    tool = tool:Clone()
    tool.Parent = player.Backpack
else
    warn(string.format("Tool '%s' doesn't exist.", tostring(item))
end

Try WaitForChild() instead of FindFirstChild() when attempting to clone your object. It is possible that the tool has not been created or loaded yet. If you receive an infinite yield warning it is possible that you are trying to index an object that doesn’t exist.

1 Like