Troubles moving a clone to the correct position

Hi there!!

I’m trying to clone some items, put them into a model and move them to a set position, but am having some troubles.

The jail is successfully cloned to the workspace, but the position is its only issue. Have tried using .CFrame insstead of .Position, and by creating an empty Vector3.new but still to no avail.

local character = player.Character
if character:GetAttribute("Jailed") == nil  or character:GetAttribute("Jailed") == false then
	coroutine.resume(coroutine.create(function()
	    character:SetAttribute("Jailed", true)
		local model = Instance.new("Model")
		model.Name = character.Name .. "'s Jail"
		model.Parent = workspace
		local JailClone = game.ServerStorage.Jail:GetDescendants()
		for i = 1, #JailClone do
			local indiviudalPart = JailClone[i]
			indiviudalPart:Clone()
			indiviudalPart.Parent = model
		end
	model.Position = character.HumanoidRootPart.Position
	task.wait(60)
	model:Destroy()
	character:SetAttribute("Jailed", false)
	end))
end

Any help is greatly appreciated! Thank you!

NVM, using the following line fixed it.

model:MoveTo(character.HumanoidRootPart.Position)

Sometimes just need determination. :smile:

Ok, some more issues have arisen.

The jail cell will sometimes get the positioning slightly off if there are some collisions involved, e.g, player is next to a wall / player is stood underneath a tree. I’ve attached an image of what I mean.

jailfloating

local character = player.Character
if character:GetAttribute("Jailed") == nil  or character:GetAttribute("Jailed") == false then
	coroutine.resume(coroutine.create(function()
		character:SetAttribute("Jailed", true)
		local model = Instance.new("Model")
		model.Name = character.Name .. "'s Jail"
		model.Parent = workspace
		local JailClone = game.ServerStorage.Jail:GetDescendants()
		for i = 1, #JailClone do
			local indiviudalPart = JailClone[i]
			local partClone = indiviudalPart:Clone()
		    partClone.Parent = model
		end
		local saveWalk = humanoid.WalkSpeed
		local saveJump = humanoid.JumpHeight
		humanoid.WalkSpeed = 0
		humanoid.JumpHeight = 0
		task.wait(1)
		model:MoveTo(character.HumanoidRootPart.Position)
		task.wait(1)
		humanoid.WalkSpeed = saveWalk
		humanoid.JumpHeight = saveJump
		task.wait(5)
		model:Destroy()
		character:SetAttribute("Jailed", false)
	end))
end

I believe it’s some problem related to collisions… but I’m pretty helpless with that other than the basics so any tips would be amazing! Thank you.

Use model:PivotTo

model:PivotTo(character.HumanoidRootPart.CFrame)

Also, there is no need for a coroutine here

Adding the line of code you suggested stops the cell from surrounding the player, and now consistently appears in the same position as it is located inside of ServerStorage.

Also, the coroutine is only there as this is a small snippet for the rest of the script, all of it has been commented out while testing this so am 100% certain nothing there is having an effect on it.

Thank you for the suggestion, any other ideas what it could be?? Tinkering with CollisionGroups probably would work, but I’d much rather see that as a last resort considering how long that could take.

Where are you adding that line? It should move the model and all of its contents to wherever the humanoid root part is relative to the mode’s pivot

Basically,this is a collision problem.

And also use primary parts position

Yes I know that MoveTo takes into account collisions. PivotTo does not.

Why not just clone the model from ServerStorage and position it to the player? Also when you replaced model.Position with model:MoveTo(), it worked because a Model doesn’t have a Position property. Also just like the other guys said, MoveTo() avoids collision while PivotTo() does not. I recommend that you after positioning the jail to the player, you position the player in the jail just in case it got out (possibly due to lag). I also recommend that you use task.spawn() instead.

local character = player.Character
if character:GetAttribute("Jailed") == nil  or character:GetAttribute("Jailed") == false then
	task.spawn(function()
		character:SetAttribute("Jailed", true)
		local JailClone = game:GetService("ServerStorage").Jail:Clone()
		JailClone.Name = character.Name .. "'s Jail"
		JailClone.Parent = workspace
		local saveWalk = humanoid.WalkSpeed
		local saveJump = humanoid.JumpHeight
		humanoid.WalkSpeed = 0
		humanoid.JumpHeight = 0
		task.wait(1)
		JailClone:PivotTo(character:GetPivot())
		character:PivotTo(JailClone:GetPivot())
		task.wait(1)
		humanoid.WalkSpeed = saveWalk
		humanoid.JumpHeight = saveJump
		game:GetService("Debris"):AddItem(JailClone, 5) -- automatically deletes the jail after 5 seconds
		task.delay(5, character.SetAttribute, character, "Jailed", false)
	end)
end

Used this idea, and it worked as I hoped. Thank you!!!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.