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
4 years ago
Edited 4 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:

01local cool = false
02script.Parent.Touched:Connect(function(hit)
03    if hit.Parent:FindFirstChild("Humanoid") and cool == false and hit.Parent ~= script.Parent.Parent.Parent then
04        if hit.Parent.Humanoid.Health > 0 then
05            cool = true
06            if game.ServerStorage:FindFirstChild("Burn")then
07                local burn = game.ServerStorage.Burn:Clone()
08                burn.Parent = hit
09                burn.Disabled = false
10                if burn ~= nil then
11                    wait()
12                    if hit.Parent.Humanoid.Health == 0 then
13                        local name = game.Players:GetPlayerFromCharacter(script.Parent.Parent.Parent)
14                        if name:FindFirstChild("leaderstats")then
15                            name.leaderstats.Kills.Value = name.leaderstats.Kills.Value + 1
View all 25 lines...

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

01while script ~= nil do
02    wait(0.1)
03    local check = script.Parent.Parent:FindFirstChild("Humanoid"-- 'Attempt to index nil with Parent 'error here
04    if check then
05        if script.Parent.Parent.Humanoid.Health > 0 then
06            cool = true
07            local wa = Instance.new("Fire")
08            wa.Parent = script.Parent
09            for i = 1,5 do
10                script.Parent.Parent.Humanoid.Health = script.Parent.Parent.Humanoid.Health - 10
11                wait(1)
12            end
13            wa:Destroy()
14            script:Destroy()
15            cool = false
16        end
17    end
18end

Please help me fix this error. Thank you!

1 answer

Log in to vote
1
Answered by
raid6n 2196 Moderation Voter Community Moderator
4 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:

01local remoteevent = Instance.new("RemoteEvent", game.ReplicatedStorage)
02remoteevent.Name = "BurnEvent"
03local cool = false
04script.Parent.Touched:Connect(
05    function(hit)
06        if hit.Parent:FindFirstChild("Humanoid") and cool == false and hit.Parent ~= script.Parent.Parent.Parent then
07            if hit.Parent.Humanoid.Health > 0 then
08                cool = true
09                if game.ServerStorage:FindFirstChild("Burn") then
10                    local burn = game.ServerStorage.Burn:Clone()
11                    burn.Parent = hit
12                    burn.Disabled = false
13                    if burn ~= nil then
14                        wait()
15                        if hit.Parent.Humanoid.Health == 0 then
View all 30 lines...

second code

01local remoteevent = game.ReplicatedStorage.BurnEvent
02remoteevent:OnServerEvent(hit)
03while script ~= nil do
04    wait(0.1)
05    local check = hit.Parent:FindFirstChild("Humanoid")
06    if check then
07        if hit.Parent:FindFirstChild("Humanoid").Health > 0 then
08            cool = true
09            local wa = Instance.new("Fire")
10            wa.Parent = script.Parent
11            for i = 1, 5 do
12                hit.Parent:FindFirstChild("Humanoid").Health = hit.Parent:FindFirstChild("Humanoid").Health - 10
13                wait(1)
14            end
15            wa:Destroy()
16            script:Destroy()
17            cool = false
18        end
19    end
20end
Ad

Answer this question