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:
image
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

mouse.Move:Connect(function()
	if holding == true then
		
		local posToSend = Vector3.new(0, 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)
		end

		script.Parent.mousePos:FireServer(posToSend)
	end
end)
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.