Setting CFrame position is being buggy

So I have code here that is supposed to change the CFrame of a copied object to the location of the previous object that was destroyed. When I print the last objects position , it prints the correct location but when I set the CFrame to the same PRINTED position, its position isnt set to the correct position. Sorry if my English is not good, It isn’t my MAIN language.

here is my code that is placed in ServerScriptService:

local repStor = game:GetService("ReplicatedStorage")
local main = game:GetService("ReplicatedStorage"):FindFirstChild("wheat-2")
local localPosX = main:GetModelCFrame().X
local localPosY = main:GetModelCFrame().Y
local localPosZ = main:GetModelCFrame().Z
main.AncestryChanged:Connect(function(child: Instance, parent: Instance) 
	if child == main then
		print("Checkpoint 1 Conf")
		if parent == game.Workspace then
			
			wait(2)
			main:Destroy()
			local wheat3Copy = repStor:FindFirstChild("wheat-3")
			wheat3Copy:SetPrimaryPartCFrame(CFrame.new(localPosX, localPosY, localPosZ))
			wheat3Copy.Parent = game.Workspace
			
		end
	end
end)

this is because you are only calling the CFrame once, ever. The position you’re getting is the position the object was when it was first called.

GetModelCFrame is also deprecated. use GetPivot(). Also, you could just set the CFrame directly.
SetPrimaryPartCFrame() is deprecated as well, use PivotTo()

local repStor = game:GetService("ReplicatedStorage")
local main = game:GetService("ReplicatedStorage"):FindFirstChild("wheat-2")
main.AncestryChanged:Connect(function(child: Instance, parent: Instance) 
	if child == main then
		print("Checkpoint 1 Conf")
		if parent == game.Workspace then
			
			wait(2)
            local CFrame = main:GetPivot()
			main:Destroy()
			local wheat3Copy = repStor:FindFirstChild("wheat-3")
			wheat3Copy:PivotTo(CFrame)
			wheat3Copy.Parent = game.Workspace
			
		end
	end
end)
1 Like

Oh my goodness, I made the most basic mistake, thank you so much, I appreciate your support!

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