uhm, maybe in the starter player and also, don’t forget to change the directory of the variable part in the above code, if it’s not the same or gives error
Updated script below, since this one was wrong
ye we could do that too
3030
still doesn’t move the part
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local part = workspace.Parta
local following = false
function Clicked()
following = not following
end
part.ClickDetector.MouseClick:Connect(Clicked)
while true do
wait()
if following then
local deltaTime = game:GetService("RunService").DeltaTime
part:SetPrimaryPartCFrame(part:GetPrimaryPartCFrame():Lerp(mouse.Hit.p, deltaTime * 5))
end
end
Is the directory of the part correct?
3030
I edit this to make it follow the mouse instead of just update position on click.
yes
303030303030303030303030303030
send me the screenshot of the error coming…
DeltaTime is not a valid member of RunService “Run Service”
thats the error from the output
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local part = workspace.Parta
local tracking = false
part.ClickDetector.MouseClick:Connect(function()
if not tracking then
tracking = true
end
end)
mouse.Button1Click:Connect(function()
if tracking then
tracking = false
end
end)
mouse.Move:Connect(function()
if tracking then
part.Position = mouse.Hit.Position
end
end)
-- Localscript in PlayerGui
I think this is what you want. If you click the part, it starts following your mouse. Then if you click anywhere, the tracking stops. It uses mouse.Move to update the position, not RenderStepped.
This will cause the part not to update if you move your camera but not your mouse, you can change to a RenderStepped event if you want that too.
try this first doge, if this doesn’t work too, then I will try to find the final solution
well now I have the issue of getting it into playergui
final solution is on the way to reach you in my next reply…
Just place it in StarterGui. You can put it in a ScreenGui and set it’s ResetOnSpawn property to false to keep this script running and never stop. If you reset and this script is directly in startergui, it will stop and replace itself when you respawn.
Here is a sample code in Roblox Lua that demonstrates how you can create a system that allows you to click on a part and have it follow the mouse smoothly, and then stop when you click again:
-- Create a global variable to store the target part
local targetPart = nil
-- Create a function to handle mouse events
local function onMouseButton(mouse)
-- Check if the left mouse button was pressed
if mouse.button == Enum.UserInputType.MouseButton1 then
-- Check if the target part is currently set
if targetPart then
-- Unset the target part
targetPart = nil
else
-- Get the part that the mouse is currently hovering over
local target = mouse.target
-- Check if the target is a valid part
if target and target:IsA("BasePart") then
-- Set the target part
targetPart = target
end
end
end
end
-- Connect the mouse button event to the function
game:GetService("UserInputService").InputBegan:Connect(onMouseButton)
-- Create a function to update the target part's position
local function updatePart()
-- Check if the target part is set
if targetPart then
-- Get the current mouse position
local mousePosition = game:GetService("UserInputService"):GetMouseLocation()
-- Set the target part's position to the mouse position
targetPart.Position = mousePosition
end
end
-- Run the update function every frame
game:GetService("RunService").RenderStepped:Connect(updatePart)
To use this code, simply copy and paste it into a script in your Roblox project, and then click on a part in the game to start moving it with the mouse. Click again to stop moving the part.
This returns a Vector2, not a position in the world the mouse is pointing to.
You are correct that the GetMouseLocation
function returns a Vector2
value, which represents the position of the mouse cursor on the screen. To get the position of the mouse cursor in the 3D world of your Roblox game, you can use the Camera
object’s ScreenPointToRay
method. This method takes a Vector2
value as input (the position of the mouse cursor on the screen) and returns a Ray
value, which represents a line that starts at the camera’s position and points in the direction of the mouse cursor.
Here is an example of how you can use the ScreenPointToRay
method to get the position of the mouse cursor in the 3D world of your Roblox game:
local mousePosition = game:GetService("UserInputService"):GetMouseLocation()
local camera = game.Workspace.CurrentCamera
local ray = camera:ScreenPointToRay(mousePosition.X, mousePosition.Y)
local hit, position = game.Workspace:FindPartOnRay(ray)
-- Check if the ray hit a part
if hit then
-- Set the target part's position to the position of the hit
targetPart.Position = position
else
-- Set the target part's position to the end of the ray
targetPart.Position = ray.Origin + ray.Direction * 100
end
This code first gets the mouse position, then gets the current camera object and uses it to create a ray that starts at the camera’s position and points in the direction of the mouse cursor. It then uses the FindPartOnRay
function to find the first part that the ray hits. If a part is found, the target part’s position is set to the position of the hit. If no part is found, the target part’s position is set to the end of the ray.
I hope this helps! Let me know if you have any questions.
Are you using an AI to generate responses…? That’s what it looks like. Forget what I said if you’re not.
bru… i am not using ai generated responses…
303030303030