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

Why won't my cloning script work?

I'm trying to have a part clone every second and move 10 units on the X Axis. Is there a way to clone something and have it move 10 units in a direction?

 while true do
wait(1)
f = script.Parent:Clone(game.Workspace)
r = script.Parent.Position.X
f.Position = (r - 10)
end

1 comment

0

All 4 answers have different information, please look at all the answers so you understand your mistake and won't make the same one next time!

4 answers

✓ Accepted answer
--f is the original part
--c is the new cloned part
while true do
wait(1)
f = script.Parent
fpos =  f.Position
c = f:Clone()
c.Parent = game.Workspace
c.Position = Vector3.new(fpos.X - 10, fpos.Y, fpos.Z)
end

If this doesn't work, let me know and I'll try to fix the script. If it does work, let me know! Good luck on your scripting journey!

3 comments

0

Just letting you know, but you forgot a '.' between c and Position. This script should work besides that.

0

This script clones it but it doesnt move the position.

0

@ dyer Thanks for letting me know! @attack The script should work now!

You had a few mistakes in your script. Anyways, here's the fixed one, and I'll explain your mistakes after the script:

while true do
	wait(1)
	f = script.Parent:Clone()
	f.Parent = game.Workspace
	r = script.Parent.Position
	f.CFrame = CFrame.new(r)*CFrame.new(-10,0,0)
end

So, in your script you had this:

f.Position = (r - 10)

This in incorrect because the game needs you to specify what type of data you're using. Here, I fixed it, and put "CFrame.new" before to specify it. "CFrame" is the same as "Vector3" which specifies location, but "CFrame" can be used more flexibly, which is why I decided to switch to that.


f = script.Parent:Clone(game.Workspace)

Your other error was when you put the parent of the cloned part inside the cloning methods parentheses. This doesn't work for whatever reason, so you would need to set the parent outside of that.


Anyways, I really hope I helped clarify some things for you. Good luck scripting, and I hope I helped :P

1 comment

0

Um dude, there was nothing wrong with f.Position = Vector3.new(r - 10). You are allowed to leave it like that.

I don't think you can put the location you want to clone the object to into the clone method, unfortunately, I fixed it below for you by adding the extra line which will parent f into workspace. Also, make it a valid position with CFraming

 while true do
wait(1)
f = script.Parent:Clone() --Can't put clone parent here
f.Parent = game.Workspace -- Added parent line here
r = script.Parent.Position.X
f.CFrame = CFrame.new(r)*CFrame.new(-10,0,0) -- CFraming
end

2 comments

0

You would also need to fix the Positioning of it. He didn't put the "Vector3" there.

0

o yea, ill fix that right now

For cloning, there are no parameters. So there is no script.Parent:Clone(game.Workspace). Also, if you leave only one number(in this case it's the part's X axis - 10), only the X axis will be set and the rest of the values like y and z will be 0 on default.


#Clone To set the parent of a cloned part, just set the parent like how you usually do:

f = script.Parent:Clone()
f.Parent = workspace --Or game.Workspace
--or
script.Parent:Clone().Parent = workspace --Or game.Workspace

#Positions Use CFrame instead incase there is a part in the way. Use Vector3.new or CFrame.new. You just can't have random numbers everywhere.

f.Position = Vector3.new(r - 10) --only changes the X

If you want to change the other values add the Y and Z values

f.Position = Vector3.new(r - 10, 0, 50)

If you want to change it RELATIVE to the original part's position:

f.Position = (f.Position.X,f.Position.Y+10,f.Position.Z ) --Moves the part up 10 studs.


#Final Product

while wait(1) do
	local p = script.Parent:Clone()
	p.Parent = workspace
	local pos = p.Position
	p.CFrame = CFrame.new(p.X-10,p.Y,p.Z)
end



Hope it helps!

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