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

Attempt to index nil with Parent?

Asked by
naturedat 124
3 years ago
Edited 3 years ago

Hi, I'm trying to make a fire sword and when it it activated on the second time, it will make the opponent burn. This is the the burning script in the firesword tool:

local cool = false
script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") and cool == false and hit.Parent ~= script.Parent.Parent.Parent then
        if hit.Parent.Humanoid.Health > 0 then
            cool = true
            if game.ServerStorage:FindFirstChild("Burn")then
                local burn = game.ServerStorage.Burn:Clone()
                burn.Parent = hit
                burn.Disabled = false
                if burn ~= nil then
                    wait()
                    if hit.Parent.Humanoid.Health == 0 then
                        local name = game.Players:GetPlayerFromCharacter(script.Parent.Parent.Parent)
                        if name:FindFirstChild("leaderstats")then
                            name.leaderstats.Kills.Value = name.leaderstats.Kills.Value + 1
                            name.leaderstats.Coins.Value = name.leaderstats.Coins.Value + 1
                        end
                    end
                end
            end
            script.Disabled = true
            cool = false
        end
    end
end)

This will duplicate a burning script in my ServerStorage and send it to the hit part. here is the script:

while script ~= nil do
    wait(0.1)
    local check = script.Parent.Parent:FindFirstChild("Humanoid")  -- 'Attempt to index nil with Parent 'error here
    if check then
        if script.Parent.Parent.Humanoid.Health > 0 then
            cool = true
            local wa = Instance.new("Fire")
            wa.Parent = script.Parent
            for i = 1,5 do
                script.Parent.Parent.Humanoid.Health = script.Parent.Parent.Humanoid.Health - 10
                wait(1)
            end
            wa:Destroy()
            script:Destroy()
            cool = false
        end
    end
end

Please help me fix this error. Thank you!

1 answer

Log in to vote
1
Answered by
raid6n 2196 Moderation Voter Community Moderator
3 years ago

The problem is that it can't find the parent of the game.

script.Parent = ServerStorage script.Parent.Parent = game script.Parent.Parent.Parent = nil

And you're also trying to find the player's humanoid inside server storage, which humanoid is not one of server storage children.

Fortunately, we can use the data from the first script to get the player's humanoid using remote events.

No changes need to be made except pasting the script.

First code:

local remoteevent = Instance.new("RemoteEvent", game.ReplicatedStorage)
remoteevent.Name = "BurnEvent"
local cool = false
script.Parent.Touched:Connect(
    function(hit)
        if hit.Parent:FindFirstChild("Humanoid") and cool == false and hit.Parent ~= script.Parent.Parent.Parent then
            if hit.Parent.Humanoid.Health > 0 then
                cool = true
                if game.ServerStorage:FindFirstChild("Burn") then
                    local burn = game.ServerStorage.Burn:Clone()
                    burn.Parent = hit
                    burn.Disabled = false
                    if burn ~= nil then
                        wait()
                        if hit.Parent.Humanoid.Health == 0 then
                            local name = game.Players:GetPlayerFromCharacter(script.Parent.Parent.Parent)
                            if name:FindFirstChild("leaderstats") then
                                name.leaderstats.Kills.Value = name.leaderstats.Kills.Value + 1
                                name.leaderstats.Coins.Value = name.leaderstats.Coins.Value + 1
                                emoteevent:FireServer(hit)
                            end
                        end
                    end
                end
                script.Disabled = true
                cool = false
            end
        end
    end
)

second code

local remoteevent = game.ReplicatedStorage.BurnEvent
remoteevent:OnServerEvent(hit)
while script ~= nil do
    wait(0.1)
    local check = hit.Parent:FindFirstChild("Humanoid")
    if check then
        if hit.Parent:FindFirstChild("Humanoid").Health > 0 then
            cool = true
            local wa = Instance.new("Fire")
            wa.Parent = script.Parent
            for i = 1, 5 do
                hit.Parent:FindFirstChild("Humanoid").Health = hit.Parent:FindFirstChild("Humanoid").Health - 10
                wait(1)
            end
            wa:Destroy()
            script:Destroy()
            cool = false
        end
    end
end

Ad

Answer this question