I have no idea what the problem is but all I know is that, it is not working right. If you see a problem that is messed up, please fix. Thanks. Btw, this script is not in a tool.

function Shoot()
	Shooting = true
	repeat
	local Ray = Ray.new(Muzzle.CFrame.p,(Mouse.Hit.p - Muzzle.CFrame.p).unit * 999) 
	local Hit,Position = game.Workspace:FindPartOnRay(Ray, User) 
	if Hit then 
		if Hit.Parent:FindFirstChild("Humanoid") then 
			Hit.Parent.Humanoid:TakeDamage(5) 
		end 
	end 
	local RayPart = Instance.new("Part", User)
	RayPart.Name = "RayPart" 
	RayPart.BrickColor = BrickColor.new("Bright red")
	RayPart.Transparency = 0
	RayPart.Anchored = true 
	RayPart.CanCollide = false 
	RayPart.formFactor = Enum.FormFactor.Custom 
	local Distance = (Position - Muzzle.CFrame.p).magnitude 
	RayPart.Size = Vector3.new(0.2,0.2,Distance) 
	RayPart.CFrame = CFrame.new(Position, Muzzle.CFrame.p) * CFrame.new(0,0,-Distance/2) 
	Gun.Fire:Play()
	Muzzle.Light.Enabled = true
	wait(.1)
	Muzzle.Light.Enabled = false
	game.Debris:AddItem(RayPart, .1) 
	until Shooting == false
end

2 comments

1

Could you elaborate on "not working right"?

0

^ Would really help with a solution. At a glance there doesn't seem to be anything syntactically incorrect, but whenever you're attempting to find a vector from point A to B, the equation is (B - A). Use that on 04 and you won't need to negate the -Distance/2.