Script:
local tool = script.Parent
tool.Activated:Connect(function()
local Owner = tool.Parent.Name
local player = game.Players:FindFirstChild(Owner)
local mouse = player:GetMouse()
local ray = Ray.new(tool.Barrel.CFrame.p, (mouse.Hit.p - tool.Barrel.CFrame.p).unit*300)
local hit, position = game.Workspace:FindPartOnRay(ray, Owner)
script.Parent.Handle.Fire:Play()
local humanoid = hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid")
if humanoid then
humanoid:TakeDamage(15)
end
local distance = (position - tool.Barrel.CFrame.p).magnitude
local rayPart = Instance.new("Part", Owner)
rayPart.Name = "RayPart"
rayPart.BrickColor = BrickColor.new("White")
rayPart.Material = "SmoothPlastic"
rayPart.Transparency = 0.5
rayPart.Anchored = true
rayPart.CanCollide = false
rayPart.TopSurface = Enum.SurfaceType.Smooth
rayPart.BottomSurface = Enum.SurfaceType.Smooth
rayPart.formFactor = Enum.FormFactor.Custom
rayPart.Size = Vector3.new(0.2, 0.2, distance)
rayPart.CFrame = CFrame.new(position, tool.Barrel.CFrame.p) * CFrame.new(0, 0, -distance/2)
game.Debris:AddItem(rayPart, 0.02)
end)
The Error: Players.Player1.Backpack.gun.Gun:7: attempt to index nil with ‘Hit’