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

if I search for the humanoid it returns a nil error and breaks the whole script?

Asked by 2 years ago

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)
0
do NOT use Instance.new("Part", workspace) proROBLOXkiller5 112 — 2y
0
parenting your part like this is slow proROBLOXkiller5 112 — 2y
0
not that it will fix the error but its just a suggestion to always do "object.parent" instead proROBLOXkiller5 112 — 2y
0
you havent given your bullet any information about what its "Parent" might be i guess (line 6) proROBLOXkiller5 112 — 2y
0
also you made a typo saying its line 16 instead of line 6 proROBLOXkiller5 112 — 2y

1 answer

Log in to vote
0
Answered by
sifn 70
2 years ago

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
Ad

Answer this question