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

How can I make a beam go to the mouse by positioning the attachment?

Asked by
MuN8MuN 47
4 years ago

Hi! :D I have a tool that will send a beam from its tip called BeamPart to the mouse.Hit . I don't want to create an additional part to attach the beam to it, I want to use the Attachment.Position property. Both attachments and the beam are parented to BeamPart, one attachment should permanently stay on the BeamPart, and the other one's Position property should change to match the mouse.Hit . But I am not that good with math, especially vector math, so none of my solutions worked. Here's my code:

  • BeamPart
    • Beam
    • tarAtt -- The moving attachment
    • stAtt -- The stationary attachment
local pl = game.Players.LocalPlayer
local mouse = pl:GetMouse()
local tool = script.Parent

enabled = true
if enabled then
    local function move()
        tool.BeamPart.tarAtt.CFrame = CFrame.new(Vector3.new(mouse.Hit.X, mouse.Hit.Y, mouse.Hit.Z)):ToObjectSpace(tool.BeamPart.CFrame)
    end
    mouse.Move:connect(move)
end

1 answer

Log in to vote
0
Answered by
IcyMizu 122
4 years ago

so lets start off with why are u doing this in a localscript ?????? u should use remoteevents for this

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local tool = script.Parent
local enabled = true
local remote = game.ReplicatedStorage.RemoteEvent
if enabled then
    Remote:FireServer(script.Parent,mouse.hit) -- firing the remote telling it has to go to the server now
end

now in server script i suggest making a part with the attachments

local remote = game.ReplicatedStorage.RemoteEvent

remote.OnServerEvent:Connect(function(plr,Tool,Mouse)--[[everything under here and above end) will execute when remote fired / tool is the script.Parent u see in fireserver brackets same for mouse.Hit]]
local a = Instance.new("Attachment") -- making a attachment and placing it into beampart
a.Parent = tool.BeamPart

local b = Instance.new("Part")--making a part to spawn it at mouse . hit
b.CFrame = Mouse
b.Transparency = 1 -- setting transparency to 1 so u cant see the part
b.CanCollide = false -- making it so u can walk trough it
b.Parent = game.Workspace

local c = Instance.new("Attachment")-- storing this attachment in b
c.Parent = b

local beam = Instance.new("Beam") -- making a beam and conecting the 2 attachments
beam.Attachment0 = a -- using the attachment in beampart
beam.Attachment1 = c -- using the attachment in the mouse.hit part
beam.Parent = b -- doesnt really matter where u parent it
end)

if u got any questions or if this doesnt work wich i dont know it will since i diddnt try it then respond with the problem and i fix it.

0
Thanks! This is a great answer, but in my question I asked if the beam could be done without a part, using the Attachment's Position property. Is your way to do this (Adding a part) more efficient or was there another reason to do it that way, or did you just misread? MuN8MuN 47 — 4y
0
just change to attachements position to mouse.hit.p Luka_Gaming07 534 — 4y
Ad

Answer this question