I'm trying to make a gun script but I got confused so I copy pasted a script from the wiki with the intent to modify it to my needs. I do understand more about raycasting after I've done this but i'm stuck as i'm trying to make a bullet move towards the location, not teleport to it. It does damage the player but the bullet is nowhere to be seen.
local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
tool.Equipped:connect(function(mouse)
print("MEN!")
mouse.Button1Down:connect(function()
local ray = Ray.new(tool.muzzle.CFrame.p, (mouse.Hit.p - tool.muzzle.CFrame.p).unit * 300)
local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)
local beam = Instance.new("Part", workspace)
beam.BrickColor = BrickColor.new("Cool yellow")
beam.FormFactor = "Custom"
beam.Material = "Neon"
beam.Transparency = 0.25
beam.Anchored = true
beam.Locked = true
beam.CanCollide = false
local distance = (tool.muzzle.CFrame.p - position).magnitude
beam.Size = Vector3.new(0.3, 0.3, 1)
beam.Velocity = distance * Vector3.new(.1,.1,.1)
local light = Instance.new("SpotLight")
light.Parent = beam
game:GetService("Debris"):AddItem(beam, 60)
if part then
local humanoid = part.Parent:FindFirstChild("Humanoid")
if not humanoid then
humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
end
if humanoid then
humanoid:TakeDamage(30)
end
end
end)
end)
Original script in WIKI https://www.robloxdev.com/articles/Making-a-ray-casting-laser-gun-in-Roblox

4 comments
Have you tried setting "beam.Anchored = false?"
That would make it fall. And the problem is on line 24. The Y axis should be 'distance'z
Same result if I put distance on the y axis
I meant Z axis, sorry