Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 7 years ago

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

Log in to vote
1
Answered by
cabbler 1942 Moderation Voter
7 years ago

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)
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.) chess123mate 5873 — 7y
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. cabbler 1942 — 7y
Ad
Log in to vote
0
Answered by 7 years ago

Nvm I got it working, thanks to pluginfactory.

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 chess123mate 5873 — 7y

Answer this question