Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Ray sometimes ignore what it hits and does not do any damage? (Gun)

Asked by 5 years ago
local player = game.Players.LocalPlayer

local UIP = game:GetService("UserInputService")

local Mouse = player:GetMouse()

local Ammo = 30



function Shoot()

if Ammo > 0 and script.Parent.Equipped then

local ray = Ray.new(script.Parent.Handle.CFrame.Position, (Mouse.Hit.Position - script.Parent.Handle.CFrame.Position))

local Hit,position = workspace:FindPartOnRayWithIgnoreList(ray,{script.Parent.Handle})

local magnitude = (script.Parent.Handle.Position - Mouse.Hit.Position).Magnitude

if Hit ~= nil then

print(Hit.Parent.Name)

if Hit.Parent:FindFirstChild("Humanoid") then

Hit.Parent.Humanoid:TakeDamage(5)

elseif Hit.Parent.Parent:FindFirstChild("Humanoid") then

Hit.Parent.Parent.Humanoid:TakeDamage(5)

end

end

end

end





function Check()

while MB1Down == true do

wait(0.1)

if Ammo > 0 then

script.Parent.Handle["G36 Single Fire"]:Play()

Ammo = Ammo - 1

Shoot()

end

end

end



UIP.InputBegan:Connect(function(Key)

if Key.KeyCode == Enum.KeyCode.R then

Ammo = 30

end

end)





Mouse.Button1Down:Connect(function()

MB1Down = true

Check()

end)



Mouse.Button1Up:Connect(function()

MB1Down = false

Check()

end)

It does not register every bullet, it sometimes skips and returns nil. I have seen other free models gun work, but why does mine not work properly?

Answer this question