Attempt to Index Nil with Clone Error

Hello,

I keep getting the error “Attempt to index nil with clone”. I am trying to make a clone of the winning player at the position of the part called character pos but it won’t make the clone and idk why

Code producing the error


			-- Game ends here
				local winnerName = finishedPlayersTable[1]
				local winnerTime = finishedTimesTable[1]
				local winnerCharacther = GetCharacter(winnerName, true)
				local winnerCharactherClone = finishedPlayersTable[1]:Clone()
				winnerCharactherClone.CFrame = workspace.EmoteParts.CharactherPos.CFrame
				game.ReplicatedStorage.EmoteCamera:FireAllClients(winnerCharactherClone)
	

Full Code (just in case)

local playersTable = {}
local totalPlayers = 0
local remoteEvent = game.ReplicatedStorage.GameConditionsToClient
local removeGui = game.ReplicatedStorage.HideGameConditionGui
local gameFinishRM = game.ReplicatedStorage.GameFinish

local formatScript = require(game.ReplicatedStorage.FormatNumbers)

local roundLength = 30
local lobbyTime = 5

game.Players.PlayerAdded:Connect(function(plr)
	table.insert(playersTable, plr.Character)
end)

game.Players.PlayerRemoving:Connect(function(plr)
	table.remove(playersTable, table.find(playersTable, plr.Character))
end)



local gameState = script.GameState

--function checkGameConditions()
--if table.maxn(playersTable) >= 2 then
--	gameState.Value = "Game Starting"
--	task.wait(10)
--	removeGui:FireAllClients()
--else
--	gameState.Value = "Waiting For More Players"

--end
--return gameState.Value
--end

--while true do
--	task.wait()
--	remoteEvent:FireAllClients(checkGameConditions())
--end

local RunService = game:GetService("RunService")

local startTime = nil
local running = false
local finishedPlayersTable = {}
local finishedTimesTable = {}

RunService.Heartbeat:Connect(function()
	if running and startTime ~= nil then
		game.ReplicatedStorage.ServerRaceTime:FireAllClients(string.format("%.1f", tick()-startTime))

	end
end)

local stopWatch = game.ReplicatedStorage.StopStopWatch
local sendTimeRM = game.ReplicatedStorage.SendTimeToClient


local playersTouched = 0

local Players = game:GetService("Players")

local function GetCharacter(name, waitForCharacter) --[[ waitForCharacter is a bool telling us whether you 
want to wait for the character if it doesnt exist ]]
	local player = game.Players:FindFirstChild(name)
	if not player then return nil end
	if waitForCharacter then
		local character = player.Character or player.CharacterAdded:Wait()
		return character
	else
		local character = player.Character
		return character
	end
end




while true do
	task.wait()
if table.maxn(game.Players:GetPlayers()) >= 1 then
	for _, v in game.Players:GetPlayers() do
		if v.Character then
			
workspace.Map["Finish Line"].Touched:Connect(function(hit)
	print("touched")
	if hit.Parent:FindFirstChild("Humanoid") then
		stopWatch:FireClient(game.Players:GetPlayerFromCharacter(hit.Parent))

		if not hit.Parent:FindFirstChild("Finished") then
			local finished = Instance.new("BoolValue", hit.Parent)
			finished.Name = "Finished"		
			table.insert(finishedPlayersTable, hit.Parent)
			table.insert(finishedTimesTable, tick()-startTime)
			print(finishedPlayersTable)
			print(finishedTimesTable)
		end
	end
end)

			for _, part in v:GetChildren() do
					v.Character.Humanoid.WalkSpeed = 0
					v.Character.HumanoidRootPart.CFrame = workspace.Map["Starting Line"].CFrame	-- Teleport To Start Line
			end
				for i = 3, 0, -1 do -- Count Down
					task.wait(1)
					remoteEvent:FireAllClients("Race Starts in "..tostring(i))
				end
				-- Game Starts Here
				if not running then
					table.clear(finishedPlayersTable)
					table.clear(finishedTimesTable)
					startTime = tick()
					running = true
					print(startTime)
				end	
				if v.Character:FindFirstChild("Finished") then
					v.Character:FindFirstChild("Finished"):Destroy()
				end
				v.Character.Humanoid.WalkSpeed = 50
				game.ReplicatedStorage.StartStopWatch:FireAllClients()
			for i = roundLength, 0, -1 do -- Count Down
			task.wait(1)
			remoteEvent:FireAllClients(formatScript.convertToMinutesAndSeconds(i))
			end
			-- Game ends here
				local winnerName = finishedPlayersTable[1]
				local winnerTime = finishedTimesTable[1]
				local winnerCharacther = GetCharacter(winnerName, true)
				local winnerCharactherClone = finishedPlayersTable[1]:Clone()
				winnerCharactherClone.CFrame = workspace.EmoteParts.CharactherPos.CFrame
				game.ReplicatedStorage.EmoteCamera:FireAllClients(winnerCharactherClone)
				
				function openAndCloseGui()
					game.ReplicatedStorage.TransitionGui:FireAllClients()
				task.wait(2)
				game.ReplicatedStorage.TransitionGui:FireAllClients()
				end
				task.spawn(openAndCloseGui)
			startTime = nil
			running = false
				for _, part in v:GetChildren() do
					v.Character.HumanoidRootPart.CFrame = workspace.LobbyPart	.CFrame-- Teleport To Lobby Line
				end
				game.ReplicatedStorage.StopStopWatch:FireAllClients()
			
				repeat 
					task.wait()
				until false
				for i = lobbyTime, 0, -1 do -- Count Down
					task.wait(1)
					remoteEvent:FireAllClients("Game will begin in "..i)
				end
	
		end
	end
	else -- 2 Players not Found
		remoteEvent:FireAllClients("Waiting For Players")
	end
end

Thanks for any help btw I do have Archivable set to true on the character

1 Like

As a suggestion have you add a safeguard if there is no winner? That seems to be the problem as there is sometimes no winner in the finisherplayers table

I planned on adding that but currently there is always a winner because the track is so short. I wanted to make sure that was working first.

This is the location of the error.
The code is essentially:

firstIndex = finishedPlayersTable[1] -- this is nil
cloned = firstIndex:Clone() -- you are performing the :Clone() operation on nil, you can only :Clone() on existing instances
local winnerCharactherClone = cloned -- this part never finishes

You mentioned you’re just getting it working right now. Here is what I recommend you do.

Have the code handle the empty table issue and have it print a FIXME output

local firstIndex = finishedPlayersTable[1] -- this is nil
if not firstIndex then
	warn("finishedPlayersTable is empty!", finishedPlayersTable) -- TODO: fill in the finishedPlayersTable
	firstIndex = tempPart -- you need to assign this to your own temp object
	-- return -- you can also just abort the code if the table is missing
end

local winnerCharactherClone = firstIndex:Clone() -- code resumes as usual
1 Like

Sorry it took me a while to respond but the finishedPlayersTable actually doesn’t have a nil value when printing but the problem is with the Clone() function. I’m just doing a quick reply rn. But when just teleporting the player themselves it works just fine however I want all players to be in the lobby when the animation is playing. Thanks for helping though.

you cant clone player characters

as a workaround try making a rig and set its humanoid description to the players humanoid description

I actually figured out the problem a while ago. I can’t remember how I fixed it but thanks for your help.

if you set the character to archivable = true would it allow you to?

1 Like

Yes it does allow you to clone the character that was the first step I took so I could duplicate the character.