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

FindPartOnRay quick question!!!! ?

Asked by
LevelKap 114
9 years ago

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

1 answer

Log in to vote
2
Answered by 9 years ago
       local hit, position = game.Workspace:FindPartOnRay(ray, user)

Hit is the part that intercepts with the Ray

Position is the position where it was intercepted.

Ray is the ray that is being fired

User is the Character of the person firing it.

Basically, to use :FindPartOnRay(), you need 2 things. The user (Character) and the Ray. Those 2 are represented by ray, user These 2 variables are mentioned previously in your script and put into variables. When we have both of these, and we use this function, it will return 2 things. hit,position Hit is the Part that it encounters, and Position is the Position where it encountered it and had to stop.

Ad

Answer this question