So i want to make a grab tool like lumber tycoons grab tool. The only problem is: It glitches out like h when the part is offrange.

I do not have any experience making things like this.

Localscript: (In startergui)

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local target = nil
local down = nil

function clickObj()
	if mouse.Target and mouse.Target:FindFirstChild("Drag") then
		target = mouse.Target
		mouse.TargetFilter = target
		down = true
	end
end
mouse.Button1Down:Connect(clickObj)

function mouseMove()
	if down and target then
		print("Ya")
		if target:FindFirstChild("BodyPosition") == nil then
			bp = Instance.new("BodyPosition", target)
		end
--		print(mouse.Hit)
			
		local force = target:GetMass() * game.Workspace.Gravity * 3
		bp.MaxForce = Vector3.new(force,force,force)
		local MaxDistance = 30
		local origin = plr.Character.HumanoidRootPart.Position
		local direction = (mouse.Hit.Position-origin).Unit
		local ray = Ray.new(origin, direction*MaxDistance)
		if (origin - target.Position).magnitude <= MaxDistance then
--			target.Position = Vector3.new(mouse.Hit.X,mouse.Hit.Y,mouse.Hit.Z)
			bp.Position = Vector3.new(mouse.Hit.X,mouse.Hit.Y,mouse.Hit.Z)
		else
			local origin = plr.Character.HumanoidRootPart.Position
			local direction = (mouse.Hit.Position-origin).Unit
			local ray = Ray.new(origin, direction*MaxDistance)
			local p,Pos = workspace:FindPartOnRayWithIgnoreList(ray, {target})
			bp.Position = Vector3.new(Pos)
--			local pa = Instance.new("Part",workspace)
--			pa.CanCollide = false
--			pa.Position = Pos
			print(Pos)
		end
	end
end
mouse.Move:Connect(mouseMove)

function mouseUp()
	down = false
	target = nil
	mouse.TargetFilter = nil
end
mouse.Button1Up:Connect(mouseUp)

Note: Yeah, This IS a repost. I wont put it in the title because most of the people will avoid it. Dont you think people abandon questions? And no, This is not against the guidelines.

2 comments

0

You could just use mouse.Hit.Position to find the position to move the part to and do bp.Position = mouse.Hit.Position

0

Thats not what this question is about.