The error is on line 16. The error outputted is: 'attempt to index nil with 'Parent'' Even if I put it inside an if statement, it still brings an error, and I'm not sure why.
local rs = game:GetService("ReplicatedStorage") local events = rs:WaitForChild("Events") local shootEvent = events:WaitForChild("Shoot") function newBullet(player,part,pos,damage) if part.Parent:FindFirstChild("Humanoid") ~= nil then local humanoid = part.Parent:FindFirstChild("Humanoid") if humanoid.Health > 0 then humanoid:TakeDamage(damage) end else local bullet = Instance.new("Part",workspace:WaitForChild("Ignore")) bullet.Anchored = true bullet.CanCollide = false bullet.BrickColor = BrickColor.new("Black") bullet.Position = pos bullet.Size = Vector3.new(0.1,0.1,0.1) game:GetService("Debris"):AddItem(bullet,5) end end shootEvent.OnServerEvent:Connect(newBullet)
It may be because the player who is firing the remote event is not actually sending a part instance, so the 'part' parameter in your function is nil. Writing part.Parent
in such a case will error the script since it is telling the script to find a humanoid object in nil.Parent
.
But why wouldn't the part ever exist? Well, in the context of guns, it can happen if the player shoots at the sky.
So a simple fix would be to verify before anything else that a part instance was indeed sent.
function newBullet(player,part,pos,damage) if part ~= nil then -- This is where we check if the part instance exists if part.Parent:FindFirstChild("Humanoid") ~= nil then local humanoid = part.Parent:FindFirstChild("Humanoid") if humanoid.Health > 0 then humanoid:TakeDamage(damage) end else local bullet = Instance.new("Part",workspace:WaitForChild("Ignore")) bullet.Anchored = true bullet.CanCollide = false bullet.BrickColor = BrickColor.new("Black") bullet.Position = pos bullet.Size = Vector3.new(0.1,0.1,0.1) game:GetService("Debris"):AddItem(bullet,5) end end end
There is also an improvement I would make to the script on Line 6.
function newBullet(player,part,pos,damage) if part ~= nil then local humanoid = part.Parent:FindFirstChild("Humanoid") -- This will return the humanoid instance if it exists, and if it doesn't then nil if humanoid ~= nil then if humanoid.Health > 0 then humanoid:TakeDamage(damage) end else -- Humanoid not found; your code that creates a bullet goes here end end else