This is a script that is supposed to be in a raycast-based gun. However, when I shoot it, nothing happens. Absolutely nothing. There isn't even anything in the output log.
The exception is when I shoot it skyward, when it registers a "Nil value" error for "Hit." This seems to indicate that it's detecting "Hit", but not doing anything afterwards. How do I fix this?
Here's the code:
local tool = script.Parent
local user
local IgnoreList = {"Handle", "Hat"}
tool.Equipped:connect(function(mouse)
user = {script.Parent:WaitForChild("Handle"), script.Parent:WaitForChild("Handle")}
mouse.Button1Down:connect(function()
local ray = Ray.new(tool.AimPart.CFrame.p, (mouse.Hit.p - tool.Handle.CFrame.p).unit*300)
local hit, position = game.Workspace:FindPartOnRayWithIgnoreList(ray, user)
local playerhit = game.Players:GetPlayerFromCharacter(hit.Parent) or game.Players:GetPlayerFromCharacter(hit.Parent.Parent)
if playerhit then
local humanoid = playerhit.Character.Humanoid
print(hit.Name)
if humanoid then
if hit.Name == "Head" then
humanoid:TakeDamage(100)
else
humanoid:TakeDamage(30)
end
end
-- Settings
local distance = (position - tool.AimPart.CFrame.p).magnitude
local rayPart = Instance.new("Part", user)
rayPart.Name = "RayPart"
rayPart.BrickColor = BrickColor.new("Really blue")
rayPart.Parent = game.Workspace
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.AimPart.CFrame.p) * CFrame.new(0, 0, -distance/2)
game.Debris:AddItem(rayPart, 0.1)
end
end)
end)
