So I made a gun following the wiki http://wiki.roblox.com/index.php?title=Making_a_ray-casting_laser_gun
but when I shoot I can fire all the way from 1 side of the map to the other how do I implement it to where the ray stops after a certain distance?
My script
local tool = script.Parent local player = game:GetService("Players").LocalPlayer tool.Equipped:connect(function(mouse) print("Tool Equipped!") mouse.Button1Down:connect(function() print("Mouse Pressed!") local ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - tool.Handle.CFrame.p).unit * 300) local part, position = workspace:FindPartOnRay(ray, player.Character, false, true) local beam = Instance.new("Part", workspace) beam.BrickColor = BrickColor.new("Bright green") beam.FormFactor = "Custom" beam.Material = "Neon" beam.Transparency = 0.25 beam.Anchored = true beam.Locked = true beam.CanCollide = false local distance = (tool.Handle.CFrame.p - position).magnitude beam.Size = Vector3.new(0.3, 0.3, distance) beam.CFrame = CFrame.new(tool.Handle.CFrame.p, position) * CFrame.new(0,0, -distance / 2) game:GetService("Debris"):AddItem(beam, 0.1) if part then local humanoid = part.Parent:FindFirstChild("Zombie") if not humanoid then humanoid = part.Parent.Parent:FindFirstChild("Zombie") end if humanoid then humanoid:TakeDamage(script.Parent.Customization.Damage.Value) end end end) end)
By definition, a ray cannot be stopped. A ray is a line segment, starting at one point and going infinitely in the specified direction. You can however, check how far away the part is.
local tool = script.Parent local MaxRayDistance = 500 local player = game:GetService("Players").LocalPlayer tool.Equipped:connect(function(mouse) print("Tool Equipped!") mouse.Button1Down:connect(function() print("Mouse Pressed!") local ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - tool.Handle.CFrame.p).unit * 300) local part, position = workspace:FindPartOnRay(ray, player.Character, false, true) if (position - tool.Handle.CFrame.p).magnitude <= MaxRayDistance then local beam = Instance.new("Part", workspace) beam.BrickColor = BrickColor.new("Bright green") beam.FormFactor = "Custom" beam.Material = "Neon" beam.Transparency = 0.25 beam.Anchored = true beam.Locked = true beam.CanCollide = false local distance = (tool.Handle.CFrame.p - position).magnitude beam.Size = Vector3.new(0.3, 0.3, distance) beam.CFrame = CFrame.new(tool.Handle.CFrame.p, position) * CFrame.new(0,0, -distance / 2) game:GetService("Debris"):AddItem(beam, 0.1) if part then local humanoid = part.Parent:FindFirstChild("Zombie") if not humanoid then humanoid = part.Parent.Parent:FindFirstChild("Zombie") end if humanoid then humanoid:TakeDamage(script.Parent.Customization.Damage.Value) end end end end) end)
Should work. If not, let me know.