I'm making a mech and it has to fire at a certain person if you press their button. I'm testing the concept of only firing if there's a clear path to the target. This is my script below:
Arthur = game.Workspace.Arthur function RangeClear(Target2) print("Finding range") local Ray = Ray.new( script.Parent.CFrame.p, (Target2.CFrame.p - script.Parent.CFrame.p).unit * 300 ) Ignore = script.Parent.Parent hit, position = Workspace:FindPartOnRay(Ray, Ignore) rek = hit and hit.Name == "Torso" if rek then return true end end function Fire(Target3) print("Firing beam") local Beam = Ray.new( script.Parent.CFrame.p, (Target3.CFrame.p - script.Parent.CFrame.p).unit * 500 ) BeamIgnore = script.Parent.Parent Beamhit, beamposition = Workspace:FindPartOnRay(Beamhit, BeamIgnore) Humanoid = hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") if Humanoid then Humanoid:TakeDamage(50) end distance = (beamposition - script.Parent.CFrame.p).magnitude P = Instance.new("Part", game.Workspace) P.Name = "Ray" P.BrickColor = BrickColor.new("Bright red") P.Transparency = .5 P.Anchored = true P.CanCollide = false P.TopSurface = Enum.SurfaceType.Smooth P.BottomSurface = Enum.SurfaceType.Smooth P.FormFactor = Enum.FormFactor.Custom P.Size = Vector3.new(.2, .2, distance) P.CFrame = CFrame.new(beamposition, script.Parent.CFrame.p) * CFrame.new(0, 0, -distance/2) end function CheckToFire(Target) if RangeClear(Target) == true then Fire(Target) end end wait(3) print("FIRING") wait(1) CheckToFire(Arthur.Torso)
How did I fail?
EDIT: I've changed the script to this:
Arthur = game.Workspace.Arthur function RangeClear(Target2) print("Finding range") local Ray = Ray.new( script.Parent.CFrame.p, (script.Parent.CFrame.p - Target2.CFrame.p).unit * 300 ) print("Made ray") Ignore = script.Parent.Parent hit, position = Workspace:FindPartOnRay(Ray, Ignore) print("Processed") if hit.Parent:FindFirstChild("Humanoid") then print("Found") return Target2 end print("Sent value") end function Fire(Target3, Times) NOT = 0 repeat print("Firing beam") local Beam = Ray.new( script.Parent.CFrame.p, (Target3.CFrame.p - script.Parent.CFrame.p).unit * 500 ) BeamIgnore = script.Parent.Parent Beamhit, beamposition = Workspace:FindPartOnRay(Beam, BeamIgnore) Humanoid = Beamhit and Beamhit.Parent and Beamhit.Parent:FindFirstChild("Humanoid") if Humanoid then Humanoid:TakeDamage(50) end distance = (beamposition - script.Parent.CFrame.p).magnitude P = Instance.new("Part", game.Workspace) P.Name = "Ray" P.BrickColor = BrickColor.new("Bright red") P.Transparency = .5 P.Anchored = true P.CanCollide = false P.TopSurface = Enum.SurfaceType.Smooth P.BottomSurface = Enum.SurfaceType.Smooth P.FormFactor = Enum.FormFactor.Custom P.Size = Vector3.new(.2, .2, distance) P.CFrame = CFrame.new(beamposition, script.Parent.CFrame.p) * CFrame.new(0, 0, -distance/2) game.Debris:AddItem(P, .3) NOT = NOT + 1 wait(.5) until NOT == Times end function CheckToFire(Target) Check = RangeClear(Target) if Check == Target then Fire(Target, 3) end end wait(3) print("FIRING") wait(1) CheckToFire(Arthur.Torso)
and the issue appears to be that in RangeClear(), it's not sending back that it's found the target.