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”.
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
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.
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)
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.
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)
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)
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?
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.