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