I've been learning raycasting and decided to make the raycasting laser gun. Here is the script:
local tool = script.Parent
local user
tool.Equipped:connect(function(mouse)
user = tool.Parent
mouse.Button1Down:connect(function()
local ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - tool.Handle.CFrame.p).unit*300)
local hit, position = game.Workspace:FindPartOnRay(ray, user)
local humanoid = hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid")
if humanoid then
humanoid:TakeDamage(30)
end
local distance = (position - tool.Handle.CFrame.p).magnitude
local rayPart = Instance.new("Part", user)
rayPart.Name = "RayPart"
rayPart.BrickColor = BrickColor.new("Bright red")
rayPart.Transparency = 0.5
rayPart.Anchored = true
rayPart.CanCollide = false
rayPart.TopSurface = Enum.SurfaceType.Smooth
rayPart.BottomSurface = Enum.SurfaceType.Smooth
rayPart.formFactor = Enum.FormFactor.Custom
rayPart.Size = Vector3.new(0.2, 0.2, distance)
rayPart.CFrame = CFrame.new(position, tool.Handle.CFrame.p) * CFrame.new(0, 0, -distance/2)
game.Debris:AddItem(rayPart, 0.1)
end)
end)
I do not understand this line: local hit, position = game.Workspace:FindPartOnRay(ray, user) why do they assign two variables for the FindPartOnRay method? Is "hit" supposed to take the value of the "ray" parameter and is "position" supposed to take the value of the "user" parameter? Because user was already defined above and it this wouldn't make sense. Did they just do this for simplicity sake so that when we talk about the damage, we can use hit, and when talking about the rayparts position, we use position? Thanks in advance for clarification
