Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

What's wrong with this Clone script?


local player = game.Players.LocalPlayer
local Mouse = player:GetMouse()
local humanoid = player.Character.Humanoid 
local s = humanoid:LoadAnimation(game.StarterPack.HandSign.Animation)



Mouse.KeyDown:connect(function(key)
	if key:lower() == "h" and humanoid then
		s:Play()
		wait(.7)
		player:Clone()
	end
end)

Output says nothing.

Obviously, when a player presses H an animation plays, wait .7 seconds then the player is cloned. The animation plays, but I'm not being cloned. Help?

-----------------------EDITED----------------------------

wait(1)

local player = game.Players.LocalPlayer
local Mouse = player:GetMouse()
local humanoid = player.Character.Humanoid 
local s = humanoid:LoadAnimation(game.StarterPack.HandSign.Animation)





Mouse.KeyDown:connect(function(key)
game.Players.PlayerAdded:connect(function(player)
player.CharacterAdded:connect(function(character)
	local CharacterChildren = character:GetChildren() 
	for i,v in pairs(CharacterChildren) do
	local Clone = v:Clone()
	if key:lower() == "h" and humanoid then
    s:Play()
	player.Character.Humanoid.WalkSpeed = 16
	wait(.7)
	Clone.Parent = game.Workspace.Character 
	end
	end
end)

Output says: 19:54:49.675 - Error in script: 'end' expected (to close 'function' at line 13) near ''

2 comments

0

Are you trying the clone the character, or the player in the game?

0

The player... like the player with the clothes n stuff...

3 answers

✓ Accepted answer

Sadly you guys are all wrong. You see, the character is not a player file or any kind of child of the player, but instead a property. You cannot use :Clone() on properties!!

This is a misconception a lot of people get wrong. Yes, it probably was a stupid decision for roblox to make the character part of a player a property, instead of a child/file.

Anyways, let's see if we can get around this problem. One thing that we want to note before we get started is that the character always resets every time the player resets (dies/re - spawns). --To fix this, we'll add a CharacterAdded event for the player so that we'll be able to grab the most recent character of that player.

game.Players.PlayerAdded:connect(function(player) --we will first grab the player (when one is added into the game)

	player.CharacterAdded:connect(function(character) --next, we will find the most recent character of that player (every time it re - spawns or is first spawned into the game)
		--do stuff
	end)
end)

Next, we'll some how have to grab all the contents located in the character and clone those. --So we'll use a table to index (locate) all the children of the character (stuff inside character).

game.Players.PlayerAdded:connect(function(player) --we will first grab the player (when one is added into the game)

	player.CharacterAdded:connect(function(character) --next, we will find the most recent character of that player (every time it re - spawns or is first spawned into the game)

		CharacterChildren = character:GetChildren() --this will grab all the stuff within the character and fit it into a table (array)
		
	end)
end)

The last step is to find a way to go through all the values that we've stored inside the table and clone those --For this, we'd use an iteration (more on this here---> http://wiki.roblox.com/index.php?title=Generic_for ). The iteration will index each value individualy, then clone them, and finally set the parent (location) to a model name "character".

game.Players.PlayerAdded:connect(function(player) --we will first grab the player (when one is added into the game)

	player.CharacterAdded:connect(function(character) --next, we will find the most recent character of that player (every time it re - spawns or is first spawned into the game)

		CharacterChildren = character:GetChildren() --this will grab all the stuff within the character and fit it into a table (array)
		

		for i,v in pairs(CharacterChildren) do --now we will iterate through all the values inside the table (CharacterChildren)
		local Clone = v:Clone() --now we clone the object we're indexing (v)

		Clone.Parent = game.Workspace.Character --this will set the parent (location) of the object were indexing to a Model named "character" inside the workspace
			
		end
	end)
end)

3 comments

0

Thx for the wiki link dude!

0

But could you give me a wiki link also explaining the whole i,v thing?

0

The "for i,v in pairs() do" statement IS already an iteration. Anways, I understand that it's hard to learn the whole "iteration" concept (took me days to figure it out). Basicly, that form of an iteration (the i,v in pairs stuff) replaces "v" with one value inside a table (more on tables here-----> http://wiki.roblox.com/index.php?title=Table). "I" stands for the place that value is inside of the

If you're trying to clone the character of the player then this requires two thing

  1. Make sure this is a local script if you're using the LocalPlayer.
  2. It should be game.Players.LocalPlayer.Character to get the character of the player, not the player in the game itself.

you also may want to put the Humanoid code like this:

local Hum = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")

I usually do this cause when you load in to the game sometimes it takes awhile before the Humanoid is loaded

This discussion is preserved as it was. New answers, comments, and votes are closed.