so I made a gun and it shoots, if the ray hits a player it does damage blah blah blah but when it hits the skybox i get this error how do I fix it
if hit.Parent:FindFirstChild("Humanoid") then
local target = hit.Parent:FindFirstChild("Humanoid")
target:TakeDamage(5)
Ammo.Value = Ammo.Value - 1
end
Just check if the hit part is == nil or not.
(In order to provide a code example, I need the function the ray is created in.) The skybox is not a part, so the ray never hits anything.
local Tool = script.Parent
local BodyAttach = Tool:FindFirstChild("BodyAttach")
local FirePart = Tool:FindFirstChild("FirePart")
local ConfigFolder = Tool:FindFirstChild("Config")
local RemoteFolder = Tool:FindFirstChild("Remotes")
local FireRemote = RemoteFolder:FindFirstChild("Fire")
local Reload = RemoteFolder:FindFirstChild("Reload")
local ConnectM6D = RemoteFolder:FindFirstChild("ConnectM6D")
local Ammo = ConfigFolder:FindFirstChild("Ammo")
local Damage = ConfigFolder:FindFirstChild("Damage")
local ReloadTime = ConfigFolder:FindFirstChild("Reload")
local SpareAmmo = ConfigFolder:FindFirstChild("SpareAmmo")
local FireSound = FirePart:FindFirstChild("Shoot")
local ReloadSound = FirePart:FindFirstChild("Reload")
ConnectM6D.OnServerEvent:Connect(function(player)
local Motor6D = player.Character:FindFirstChild("Right Arm").RightArmMotor6D
Motor6D.Part1 = BodyAttach
end)
FireRemote.OnServerEvent:Connect(function(player, mousepos)
if Ammo.Value == 0 then
return
else
local ray = Ray.new(FirePart.CFrame.p, (mousepos - FirePart.CFrame.p).unit *150)
local hit, position = game.Workspace:FindPartOnRay(ray, player.character)
FireSound:Play()
if hit.Parent:FindFirstChild("Humanoid") then
local target = hit.Parent:FindFirstChild("Humanoid")
local character = hit.Parent
target:TakeDamage(5)
Ammo.Value = Ammo.Value - 1
end
end
end)
if hit ~= nil and hit.Parent:FindFirstChild("Humanoid") then
local target = hit.Parent:FindFirstChild("Humanoid")
local character = hit.Parent
target:TakeDamage(5)
Ammo.Value = Ammo.Value - 1
end