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
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.
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.
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.
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