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

Trying to make a mouse tracking script... Atleast was... Help?

I am relatively new to coding and am still learning... I was wondering if you could help me troubleshoot this:

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local Down = false

Mouse.Button1Down:connect(function()
	Down = true
	while Down == true do
     	local beam = Instance.new("Part", workspace)
		beam.BrickColor = BrickColor.new("Bright red")
		beam.FormFactor = "Custom"
		beam.Material = "Neon"
		beam.Transparency = 0.5
		beam.Anchored = true
		beam.Locked = true
		beam.CanCollide = false
		
		local distance = (script.Parent.Head.CFrame.p - Mouse.Hit.p)
		beam.CFrame = CFrame.new(script.Parent.Head.CFrame.p, Mouse.Hit.p) * CFrame.new(0, 0, -distance / 2)
		
		game:GetService("Debris"):AddItem(beam, 0.01)
		
		wait(0.01)
	end
end)

Mouse.Button1Up:connect(function()
	Down = false
end)

I was trying to make it draw a line from the head to the mouse that way players and friends could see what they were doing. I plan on making a GUI that says the name of what you are aiming at and the distance.

2 answers

✓ Accepted answer

You said Nvm but I already wrote this so...

I want you to take away this script efficiency rather than just a tutorial. Most importantly that there is not much need for toggling a "Down" variable nor waiting a certain time.

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()

Mouse.Button1Down:connect(function()
	local beam = Instance.new("Part", workspace)
    beam.BrickColor = BrickColor.new("Bright red")
    beam.FormFactor = "Custom"
    beam.Material = "Neon"
    beam.Transparency = 0.5
    beam.Anchored = true
    beam.Locked = true
    beam.CanCollide = false
	local conn = game:GetService('RunService').RenderStepped:connect(function()
		local p = Mouse.Hit.p
		local headp = script.Parent.Head.CFrame.p
	    local distance = (headp-p).magnitude
		beam.Size = Vector3.new(0,distance,0)
	    beam.CFrame = CFrame.new(headp:lerp(p,0.5), p)*CFrame.Angles(math.pi/2,0,0)
	end)
	Mouse.Button1Up:wait()
	conn:disconnect()
	beam:Destroy()
end)

2 comments

0

+1. Very minor suggestion/question: wouldn't listening to Heartbeat be superior (compared to RenderStepped)? I'm told that listening to RenderStepped prevents Roblox from rendering graphics (until RenderStepped is done). Further, according to a diagram I found, Heartbeat occurs right after Physics and so seems like the right time to do it. (Not a big difference either way, of course.)

0

It doesn't matter much in this case because the code is not very intensive. I chose RenderStepped because I want the part to render smoothly.

Nvm I got it working, thanks to pluginfactory.

1 comment

0

You should either accept an answer (someone else posted a script to consider) or change your question title so that it's clear this question has an answer

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