Attempt to index nil with "Clone" in a normal script inside of Workspace

Ok so I have a function that checks which players won the game and then I want to clone their character and turn it into a statue and show who won the round. But I get an error when I try to clone the character!
Here’s the script:

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		local statues = game.Workspace.Statues
		local winners = game.ReplicatedStorage.Winners
		local win1 = winners.Winner1.Value
		local win2 = winners.Winner2.Value
		local win3 = winners.Winner3.Value
		if win1.Value ~= "" then
			statues:FindFirstChild("1").name.SurfaceGui.SIGN.Text = win1
		end
		if win2.Value ~= "" then
			statues:FindFirstChild("2").name.SurfaceGui.SIGN.Text = win2
		end
		if win3.Value ~= "" then
			statues:FindFirstChild("3").name.SurfaceGui.SIGN.Text = win3
		end
		local plr1 = game.Players:FindFirstChild(win1)
		local plr2 = game.Players:FindFirstChild(win2)
		local plr3 = game.Players:FindFirstChild(win3)
		if plr1 then
			local char = game.Workspace:FindFirstChild(plr1.Name)
			local clone = char:Clone() -- attempt to index nil with "Clone", The character exists, the value of win1 is my name and my character already is in workspace
			clone.Name = "Winner1"
			clone.HumanoidRootPart.Position = statues:FindFirstChild("1").Pos.Position
			clone.HumanoidRootPart.Orientation = Vector3.new(0, 90, 0)
			clone.Parent = statues:FindFirstChild("1")
		end
		if plr2 then
			local char = game.Workspace:FindFirstChild(plr2.Name)
			local clone = char:Clone()
			clone.Name = "Winner2"
			clone.HumanoidRootPart.Position = statues:FindFirstChild("2").Pos.Position
			clone.HumanoidRootPart.Orientation = Vector3.new(0, 90, 0)
			clone.Parent = statues:FindFirstChild("2")
		end
		if plr3 then
			local char = game.Workspace:FindFirstChild(plr3.Name)
			local clone = char:Clone()
			clone.Name = "Winner3"
			clone.HumanoidRootPart.Position = statues:FindFirstChild("3").Pos.Position
			clone.HumanoidRootPart.Orientation = Vector3.new(0, 90, 0)
			clone.Parent = statues:FindFirstChild("3")
		end
	end)
end)

I can’t find other people with this problem or a way of fixing this so please help! There isn’t any other error except that it indexes nil with “Clone”.

what kind of value is win1: string value or object value?

2 Likes

it’s a string value and it has holds the player’s name that won

hmm Try this:

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		local statues = game.Workspace.Statues
		local winners = game.ReplicatedStorage.Winners
		local win1 = winners.Winner1.Value
		local win2 = winners.Winner2.Value
		local win3 = winners.Winner3.Value
		if win1.Value ~= "" then
			statues:FindFirstChild("1").name.SurfaceGui.SIGN.Text = win1
		end
		if win2.Value ~= "" then
			statues:FindFirstChild("2").name.SurfaceGui.SIGN.Text = win2
		end
		if win3.Value ~= "" then
			statues:FindFirstChild("3").name.SurfaceGui.SIGN.Text = win3
		end
		if win1 then
			local char = game.Workspace:FindFirstChild(win1)
			local clone = char:Clone()
			clone.Name = "Winner1"
			clone.HumanoidRootPart.Position = statues:FindFirstChild("1").Pos.Position
			clone.HumanoidRootPart.Orientation = Vector3.new(0, 90, 0)
			clone.Parent = statues:FindFirstChild("1")
		end
		if win2 then
			local char = game.Workspace:FindFirstChild(win2)
			local clone = char:Clone()
			clone.Name = "Winner2"
			clone.HumanoidRootPart.Position = statues:FindFirstChild("2").Pos.Position
			clone.HumanoidRootPart.Orientation = Vector3.new(0, 90, 0)
			clone.Parent = statues:FindFirstChild("2")
		end
		if win3 then
			local char = game.Workspace:FindFirstChild(win3)
			local clone = char:Clone()
			clone.Name = "Winner3"
			clone.HumanoidRootPart.Position = statues:FindFirstChild("3").Pos.Position
			clone.HumanoidRootPart.Orientation = Vector3.new(0, 90, 0)
			clone.Parent = statues:FindFirstChild("3")
		end
	end)
end)

no need to get the player and then the player’s name when win# holds the player’s name

1 Like

It has something to do with your Winner value being nil. I am not exactly sure how you change the Winner values, so I can’t really help much with it. Try looking through your scripts to see why your Winner value is coming nil. You can use print() to help debug it and find what values turn out nil.

1 Like

It still attemps to index nil with “Clone” tho!

It’s not nil, the value of it is corrent. It printed out my name so that’s not the problem.

check your win1 variable, it seems to be coming out nil, that is the only reason why you should be having nil issues.

Also I don’t see why you would say win1.Value if you are already grabbing the Value from Winner1. That is an issue.

1 Like

I changed the script to this:

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function()
		local statues = game.Workspace.Statues
		local winners = game.ReplicatedStorage.Winners
		local win1 = winners.Winner1.Value
		local win2 = winners.Winner2.Value
		local win3 = winners.Winner3.Value
		if win1 ~= "" then
			statues:FindFirstChild("1").name.SurfaceGui.SIGN.Text = win1
		end
		if win2 ~= "" then
			statues:FindFirstChild("2").name.SurfaceGui.SIGN.Text = win2
		end
		if win3 ~= "" then
			statues:FindFirstChild("3").name.SurfaceGui.SIGN.Text = win3
		end
		if win1 then
			local char = game.Workspace:FindFirstChild(win1)
			local clone = char:Clone()
			clone.Name = "Winner1"
			clone.HumanoidRootPart.Position = statues:FindFirstChild("1").Pos.Position
			clone.HumanoidRootPart.Orientation = Vector3.new(0, 90, 0)
			clone.Parent = statues:FindFirstChild("1")
		end
		if win2 then
			local char = game.Workspace:FindFirstChild(win2)
			local clone = char:Clone()
			clone.Name = "Winner2"
			clone.HumanoidRootPart.Position = statues:FindFirstChild("2").Pos.Position
			clone.HumanoidRootPart.Orientation = Vector3.new(0, 90, 0)
			clone.Parent = statues:FindFirstChild("2")
		end
		if win3 then
			local char = game.Workspace:FindFirstChild(win3)
			local clone = char:Clone()
			clone.Name = "Winner3"
			clone.HumanoidRootPart.Position = statues:FindFirstChild("3").Pos.Position
			clone.HumanoidRootPart.Orientation = Vector3.new(0, 90, 0)
			clone.Parent = statues:FindFirstChild("3")
		end
	end)
end)

But it still indexes nil with “Clone”

Is your win1 variable nil? If not then it is an issue trying to find the character. My guess would be either the character isn’t loading in time because this event is fired when a character spawns in, not when all characters are spawned in. Or it could be a problem with win1 not being a string, but you said the Winner values were string values.

1 Like

It might be because you’re trying to reference a player object with an instance. Try doing ‘Player.Character’ instead if you want to get the player’s character (‘player’ being the player you’re trying to clone)

1 Like

It doesn’t index the character with nil. It even prints out the name of the character, it just indexes nil with Clone()

Now it indexes nil with ‘Name’

So it prints out the character (char), but Clone() says char is nil? That doesn’t make any sense because you define char as an instance.

Edit: May I also ask why this is being fired over a CharacterAdded event? I don’t think that is the best event to use.

1 Like

Here’s the new script:

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function()
		local statues = game.Workspace.Statues
		local winners = game.ReplicatedStorage.Winners
		local win1 = winners.Winner1.Value
		local win2 = winners.Winner2.Value
		local win3 = winners.Winner3.Value
		if win1 ~= "" then
			statues:FindFirstChild("1").name.SurfaceGui.SIGN.Text = win1
		end
		if win2 ~= "" then
			statues:FindFirstChild("2").name.SurfaceGui.SIGN.Text = win2
		end
		if win3 ~= "" then
			statues:FindFirstChild("3").name.SurfaceGui.SIGN.Text = win3
		end
		if win1 then
			local char = plr.Character
			print(char.Name)
			local clone = char:Clone()
			clone.Name = "Winner1"
			clone.HumanoidRootPart.Position = statues:FindFirstChild("1").Pos.Position
			clone.HumanoidRootPart.Orientation = Vector3.new(0, 90, 0)
			clone.Parent = statues:FindFirstChild("1")
		end
		if win2 then
			local char = game.Workspace:FindFirstChild(win2)
			local clone = char:Clone()
			clone.Name = "Winner2"
			clone.HumanoidRootPart.Position = statues:FindFirstChild("2").Pos.Position
			clone.HumanoidRootPart.Orientation = Vector3.new(0, 90, 0)
			clone.Parent = statues:FindFirstChild("2")
		end
		if win3 then
			local char = game.Workspace:FindFirstChild(win3)
			local clone = char:Clone()
			clone.Name = "Winner3"
			clone.HumanoidRootPart.Position = statues:FindFirstChild("3").Pos.Position
			clone.HumanoidRootPart.Orientation = Vector3.new(0, 90, 0)
			clone.Parent = statues:FindFirstChild("3")
		end
	end)
end)

Now it indexes nil with “Name” at this line:

clone.Name = "Winner1"

Wait, so now you are cloning the player added into the game’s character? I thought you were trying to get the winning player’s character? Also like I asked before why is this being fired over a CharacterAdded event?

1 Like

This script was originally in a function and not in a character added event.

It’s in a CharacterAdded event for testing because I haven’t made a function yet to check if the round is over and check who won. Also when a player joins, win1’s value is automatically set to their name for testing.


The text changed but my character doesn’t clone and it indexes nil with “Name” of the clone that I want to make of my character!

by default, archivable = false with the characters of players
image
so, in your script place
char.Archivable = true
and clone

2 Likes