I need help with a telekinesis script

So I need help in creating this telekinesis script for my game. The idea is, you press e while your mouse is hovering over your target (another player) then the target will be floating and going wherever the position of your mouse is

create a attachment/part on wherever your mouse is when you press E based off raycasting, if a target is found like player then thats where the original position of the instance will be, after that you’re just constantly updating the parts positions to the direction of the mouse whilst maintain respect for the magnitude/distance from the first raycast

Hi! Sorry for the late reply but how exactly do I make the part constantly update its position? (I’m also new to scripting so I apologize)

Well, I won’t be able to really help you for the next 7-8 hours, but after that I could get on my computer and help you, I actually have already made this exact thing

renderstepped mouse position to world space raycast

1 Like

Thanks! Any help would be appreciated

I have made a LocalScript for you but there’s 3 problems

  1. It will make you drag EVERY single draggable part
  2. It constantly moves to the player’s camera, meaning you can barely do anything with your draggable part
  3. It’s sort of choppy

You can’t really use this script, but you can take a general Idea of how you’d want your script to be done:

-- Get every part in Workspace (including parts in folders, models, other parts etc.)
local descendants = game.Workspace:GetDescendants()
-- Used to detect if player presses keys
local UIS = game:GetService("UserInputService")
-- Used to constantly do an action in this context
local RS = game:GetService("RunService")
-- get the player's mouse
local mouse = game.Players.LocalPlayer:GetMouse()
-- used to not make the player spam the "E" key
local db = false

-- Do something when finding all descendants
for index, descendant in pairs(descendants) do
	--[[
	1) Go to your draggable part that you want to control with telekinesis
	2) Click on "Properties" Tab
	3) All the way down , add an ATTRIBUTE with a "Boolean" Value and name it "Draggable"
	4) Set the "Draggable" Boolean to true after making the value
	5) NOW we find the attribute in the part and we do something with it IF we find it
	]]
	local Draggable = descendant:GetAttribute("Draggable")
	
	-- if the descendant(part) is draggable, do something
	if Draggable == true then
		-- Use UIS to detect the player's keys now
		UIS.InputBegan:Connect(function(input)
			-- If player presses the "E" key
			if input.KeyCode == Enum.KeyCode.E then
				if db == false then
					db = true
					RS.RenderStepped:Connect(function()
						descendant.position = mouse.Hit.p
					end)
				end
				if db == true then
					db = false
					descendant.Position = descendant.Position
				end
			end
		end)
	end
end

Also, in cases like this, Attributes may not be the best thing to use to find the desired parts

to make the part constantly change the position, you use stuff like RenderStepped Events:

local RS = game:GetService("RunService")
RS.RenderStepped:Connect(function()
	descendant.position = -- whatever position you want it to be
end)
1 Like