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)

4 comments
This doesn't work but I found a way around it line 11 you just change the number you multiply by to say 25 it makes it not go as far thank you though
What? Why the heck does that work? Alright, that's something to look at.
because all you did was created a variable and throw in a random if statement you pretty much did nothing
Wait lol. I forgot the
.magnitude. I was asking how your changing numbers worked.