How would I make the player able to move enemies closer and further to them while using telekinesis

Here’s what I have so far:
So far it goes to the position of the mouse
I want it to come closer to the player as they hold the E key and further as they hold the Q key. I’ve tried doing this myself, but I’ve had no luck.
My code where it tells the server where to position the enemy:

local holdingE = false -- e will make them move closer
local holdingQ = false -- q will make them move further

local enemyYPos
local enemyXPos
local enemyZPos 

local tkDistance = 30
local tkMoveIncremement = .5

local lastposToSend

	if holding == true then
		local posToSend =, 0, 0)
		local origin = player.Character.Head.Position
		local direction = (mouse.Hit.Position - origin).Unit
		local result = workspace:Raycast(player.Character.Head.Position, direction*tkDistance)

		local intersection = result and result.Position or origin + direction*tkDistance

		posToSend = intersection
		local cameraDirection = workspace.Camera.CFrame.LookVector
		if holdingE == true then
			posToSend *= -posToSend * (cameraDirection*tkMoveIncremement)
		elseif holdingQ == true then
			posToSend *= posToSend * (cameraDirection*tkMoveIncremement)

1 Like

You need to change your tkDistance to make it go closer/farther. Maybe use UIS and hold the count like that?

1 Like

Oh my gosh you are totally right. I honestly don’t know how I didn’t think of that. Thanks so much!

1 Like

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