Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to make a ray that moves with the mouse?

This might be a stupid question, but I've only just started scripting, and really need this for something I'm working on. If someone could please share with me how I could make a cylinder follow the mouse, whilst still being anchored, that would be great. I've tried to search this question up on Youtube, but nothing came up ;c. Hopefully someone can help me.

Thanks!

1 comment

1 answer

In a local script that is in the StarterGui paste the following code:

local cylinder = game.Workspace:FindFirstChild("Part", true) -- Change "Part" to the name of the cylinder you want to move and make sure that the cylinder has a unique name that nothing else has --
local mouse = game.Players.LocalPlayer:GetMouse()
while cylinder == nil do
	wait(0.1)
end
mouse.TargetFilter = cylinder
while true do
    wait(0.05)
	local cf = CFrame.new(mouse.Hit.X, mouse.Hit.Y, mouse.Hit.Z)
	cylinder.CFrame = cf
	game.ReplicatedStorage.moveCylinderEvent:FireServer(cf, cylinder)
end

In the ReplicatedStorage insert a remote event and name it "moveCylinderEvent" without the quotes.

In the ServerScriptService insert a script and paste the following code:

game.ReplicatedStorage.moveCylinderEvent.OnServerEvent:Connect(function(cf, cylinder)
	cylinder.CFrame = cf
end)

5 comments

0

I just fixed it. It should work now

0

:O I'm going to try it, thank you so much for answering so quickly!! :D

0

Thx for that script, but I'd also like to know how to make it's directions and rotation follow the mouse. Like not necessarily go to the mouse, but be facing the mouse, much like the Laser Glasses script. basically a blast you can aim.

0

this is a bit inefficient, for one, if the laser is for aiming, it really shouldn't be seen by anyone but the person aiming, even if it isn't, you really shouldn't set the cframe of an object twice on supposedly the same occasion

0

To elaborate, His response wasn't the solution to my question, I'm not going to use to two CFrames, just the one to make the cylinder point in directions. And what I would need is a script to make the cylinders rotation/direction change, to make a moveable blast(in terms of it's pointing direction). I think I'm confusing everyone by saying aimable. https://www.youtube.com/watch?v=cmqWmmxyy9U - wha

This discussion is preserved as it was. New answers, comments, and votes are closed.