Trying to make a telekenisis thing - having trouble limiting the target obj's distance away from me

I’ve been trying for a bit now to limit the distance of a levitated object in my script. I use the mouse to help define the position of the object when it’s levitating, but when I move my mouse to the void the object gets flung.

Code:

function Telekenises()
	if Mouse.Target and Mouse.Target.Parent:FindFirstChild("HumanoidRootPart") then
		local RayResult = workspace:Raycast(RightHand.CFrame.p, (Mouse.Hit.p - RightHand.CFrame.p).unit * GrabDistance, TeleParams)
		local Root = Mouse.Target.Parent:FindFirstChild("HumanoidRootPart")
		local RayInst = RayResult.Instance
		if RayResult.Instance == Root or RayInst.Parent:FindFirstChild(Root) then
			local BP = Instance.new("BodyPosition")
			InstanceCache[#InstanceCache + 1] = BP
			BP.MaxForce = Vector3.new(100000, 100000, 100000)
			BP.Position = RayResult.Position
			BP.P = 10000
			BP.Parent = Root
			local MousePos = Mouse.Hit.p
			local LastPos = RayResult.Position
			print(MousePos)
			while KeyDown == true do
				local VectorLength = (Mouse.Hit.Position - Root.Position).magnitude
				local Position = Vector3.new(Mouse.Hit.Position.X, Mouse.Hit.Position.Y, Mouse.Hit.Position.Y)
				print(VectorLength)
				if VectorLength > GrabDistance then
					Position = Root.Position + Mouse.UnitRay.Direction * GrabDistance
				end
				BP.Position = Position
				RunService.Stepped:Wait()
			end
		end
	end
end
1 Like

So you’re asking how to make the telekinesis power only work inside of the players distance limit? If so you could have an invisible hit box around the player or the target object at all times (make sure it is anchored and non-collide able), if the object or player is in that hitbox then set a value to true, then in that script you showed put a if statement for if the value is true and then run the code.

I hope this helps! (:

3 Likes

Did this work for you? Just wondering.

1 Like

I don’t think my problem is detecting the player, it’s moving it to the correct position based on my mouse.

1 Like

Check if the .Target property of an MouseObject is nil, if so, don’t move the part.

If MouseObject.Target is nil, it means there’s no BasePart pointed by the Mouse.

1 Like

The mouse wouldn’t be hitting anything if it is hovering over a void, causing things to act odd. Instead you could have a constant scalar value represent the maximum value the mouse should go if it were to not hit anything. To get the new position using this scalar, you would simply apply the same method you did to position the object, by getting the mouse’s direction and multiplying it by the scalar quantity, offset from the current camera position

2 Likes
local direction = Mouse.Hit.Position - Root.Position
local limitedDirection = direction.Unit * math.min(GrabDistance, direction.Magnitude)

This will limit the direction to GrabDistance or less.

And I don’t know why you are using Mouse.UnitRay.Direction when you are already calculating the direction at VectorLength? Plus UnitRay is the ray from the Camera to the mouse, not from the player.

1 Like

That’s not what I want to have happen. I want it to move around no matter what.

Just tried that, it didn’t work. When I hover my mouse over the void before my levitated object gets there, the object still gets flung.