AlignPosition acts after a long time

ok so my align position takes a long time to be active idk why but it does that sometimes…
this is my code

local Plr = game.Players.LocalPlayer
local Mouse = Plr:GetMouse()
local RS = game:GetService("RunService")
local Target
local Equipped = false
local Mouseat = Instance.new("Attachment",workspace.Terrain)
local AP = game.Workspace.Terrain.AlignPosition
AP.Attachment1 = Mouseat
Mouseat.Visible = true
Mouseat.Name = "Mouse"
script.Parent.Equipped:Connect(function()
	Equipped = true
end)
script.Parent.Unequipped:Connect(function()
	Equipped = false
end)
local CenterAt = Instance.new("Attachment",script.Parent.Shoot)
local function setpos(SizeX,SizeY,SizeZ,Num)
	local postable = 	{
		Vector3.new(SizeX/3,SizeY/3,SizeZ/3),
		Vector3.new(SizeX/3,-SizeY/3,SizeZ/3),
		Vector3.new(-SizeX/3,SizeY/3,-SizeZ/3),
		Vector3.new(-SizeX/3,-SizeY/3,-SizeZ/3)
	}
	return postable[Num]
end


Mouse.Button1Down:Connect(function()
	if Mouse.Target and Mouse.Target:FindFirstChild("IsHoldAble") and Equipped == true then
		Target = Mouse.Target
		local partat = Instance.new("Attachment",Target)
		AP.Attachment0 = partat
		for i,v in pairs(game.Workspace.Beams:GetChildren()) do
			v.Attachment1 = CenterAt
			local at = Instance.new("Attachment",Target)
			at.Position = setpos(Target.Size.X,Target.Size.Y,Target.Size.Z,i)
			v.Attachment0 = at
		end
	end
end)
Mouse.Button1Up:Connect(function()
	if Target ~= nil then
		for i,v in pairs(Target:GetChildren()) do
			if v:IsA("Attachment") then
				v:Destroy()
			end
		end
	end
end)
Mouse.Move:Connect(function()
	local camera = game.Workspace.CurrentCamera
	local pos = camera.CFrame.Position + (Mouse.Hit.Position - camera.CFrame.Position).Unit * 20
	Mouseat.Position = pos
	if Target ~= nil and Equipped == false then
		for i,v in pairs(Target:GetChildren()) do
			if v:IsA("Attachment") or v:IsA("AlignPosition") then
				v:Destroy()
			end
		end
	end
end)

Can you please describe more about what does this code do? And wherever you suspect it’s causing the issue?

1 Like

You’re calling Instance.new() several times and using the parent parameter.

1 Like

in mouseat i can undo that in other nope

1 Like

idk where it cause issue and the code is a holding object code

1 Like

Can’t you just set the parent property explicitly?

Instance.Parent = Instance

1 Like

i did do that and am gonna test it
results: non still dosent work

1 Like
local Plr = game.Players.LocalPlayer
local Mouse = Plr:GetMouse()
local RS = game:GetService("RunService")
local Target
local Equipped = false
local Mouseat = game.Workspace.Terrain.Mouse
local AP = game.Workspace.Terrain.AlignPosition
script.Parent.Equipped:Connect(function()
	Equipped = true
end)
script.Parent.Unequipped:Connect(function()
	Equipped = false
end)
local CenterAt = Instance.new("Attachment",script.Parent.Shoot)
local function setpos(SizeX,SizeY,SizeZ,Num)
	local postable = 	{
		Vector3.new(SizeX/3,SizeY/3,SizeZ/3),
		Vector3.new(SizeX/3,-SizeY/3,SizeZ/3),
		Vector3.new(-SizeX/3,SizeY/3,-SizeZ/3),
		Vector3.new(-SizeX/3,-SizeY/3,-SizeZ/3)
	}
	return postable[Num]
end


Mouse.Button1Down:Connect(function()
	if Mouse.Target and Mouse.Target:FindFirstChild("IsHoldAble") and Equipped == true then
		Target = Mouse.Target
		local partat = Instance.new("Attachment")
		partat.Parent = Target
		AP.Attachment0 = partat
		for i,v in pairs(game.Workspace.Beams:GetChildren()) do
			v.Attachment1 = CenterAt
			local at = Instance.new("Attachment",Target)
			at.Position = setpos(Target.Size.X,Target.Size.Y,Target.Size.Z,i)
			v.Attachment0 = at
		end
	end
end)
Mouse.Button1Up:Connect(function()
	if Target ~= nil then
		for i,v in pairs(Target:GetChildren()) do
			if v:IsA("Attachment") then
				v:Destroy()
			end
		end
	end
end)
Mouse.Move:Connect(function()
	local camera = game.Workspace.CurrentCamera
	local pos = camera.CFrame.Position + (Mouse.Hit.Position - camera.CFrame.Position).Unit * 20
	Mouseat.Position = pos
	if Target ~= nil and Equipped == false then
		for i,v in pairs(Target:GetChildren()) do
			if v:IsA("Attachment") or v:IsA("AlignPosition") then
				v:Destroy()
			end
		end
	end
end)

new code

1 Like