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

what does "attempt to index local 'variable' (a nil value)" mean? Fireball explosion

Asked by 6 years ago

So i was making a fire ball which explodes upon contact with any objects that can be collided. After colliding, the fireball blows up into a sphere (Sphere grows from small to big). When the aoe explosion touches any humanoid character, my script will grab an IntValue from the lighting and put it into the target (To serve as a multi hit debounce).

Everything was working fine except when i tried to change the value of the IntValue. The first time upon touching, it grabs the IntValue from lighting and placed it inside the character model just fine, but when the code tries to change its value from the character, this error appeared: "attempt to index local 'aoecheck' (a nil value)".

After this error appeared, I tried to hit myself with the explosion aoe again but this time onward, everything worked as intended. If i were to grab that IntValue from the lighting before hitting myself with the aoe blast, it will also work. Any ideas?

localscript (No error)

--declaring shortcuts
fireball = script.Parent
player = game.Players.LocalPlayer
remote = fireball.RemoteEvent
mouse = player:GetMouse()
clickdebounce = false

fireball.Activated:connect(function()
    if clickdebounce == false then
        clickdebounce = true
        fireball.RemoteEvent:FireServer()
        wait(0.5)
        clickdebounce = false
    end
end)

script (The one with the error: Line 47)

--declaring shortcuts
fireball = script.Parent
remote = fireball.RemoteEvent
player = fireball.Parent.Parent
character = player.Character
mouse = player:GetMouse()
dmgdebounce = false
aoedebounce = false
testvalue = 0

remote.OnServerEvent:connect(function()

    local projectile = fireball.Handle:Clone()         
    local bv = Instance.new("BodyVelocity",projectile) 
    bv.MaxForce = Vector3.new(math.huge,math.huge,math.huge)           
    bv.Velocity = mouse.hit.lookVector * 50            
    projectile.Parent = game.Workspace                     
    projectile.Touched:connect(function(entity)

        local humanoid = entity.Parent:FindFirstChild("Humanoid")
        if not entity:IsDescendantOf(character) and entity.CanCollide == true then

            --FireBall Damage--
            if humanoid then                   
                humanoid:TakeDamage(10)
                print("took fireball damage")
            end

            --creating an aoe
            local aoe = game.Lighting.AOE:Clone()
            aoe.Parent = workspace
            aoe.CFrame = projectile.CFrame                             
            projectile:Destroy()                           

            --AOE Damage--
            for i = 0,10 do                                
                aoe.Touched:connect(function(aoentity)
                    local aoecounter = game.Lighting.AoeDebounce:Clone()                           
                    local aoehumanoid = aoentity.Parent:FindFirstChild("Humanoid")
                    local aoecheck = aoentity.Parent:FindFirstChild("AoeDebounce")

                    if not aoecheck then   
                        aoecounter.Parent = aoentity.Parent
                    end

                    if aoehumanoid and aoedebounce ==false and aoehumanoid.Health>0 then
                        aoecheck.Value = aoecheck.Value +1  --ERROR HERE
                        aoehumanoid:TakeDamage(10)
                        print("took aoe dmg")                  
                    end                
                end)                           
                aoe.Size = Vector3.new(i*5,i*5,i*5)        
                wait(0.05)
            end
            aoedebounce = false
            aoe:Destroy()
        end
    end)
end)

The desired goal was increasing the distributed "AoeDebounce" inside the target character by 1 every time the aoe blast touches them, without the error popping up.

Required stuff for the code:

1)Tool inside StarterPack

2)Handle inside tool

3)RemoteEvent inside tool

4)LocalScript inside tool

5)Script inside tool

6) Sphere part named "AOE" inside Lighting (Remember to anchor it and set Cancollide = false)

7) IntValue named "AoeDebounce" inside lighting

2 answers

Log in to vote
0
Answered by 6 years ago

Whenever that comes up for me, it means that you forgot something on what I like to call "The Break Down Tree". For instance you would get that error on line two if you said this:

wait(5)
Workspace.Part.Transparency  = 1

you got that because you skipped a level on the tree, the game part. So your trying to 'Globalize' or make the next level on the tree, a 'nil'(Non existent) value. Try seeing if there's a level you missed on the tree with that.

Please Accept My Answer!

0
try tracking it down, so look at Aoecheck, that is previously seen in line forty, where you mention aoenity, track that down, you use that as a parameter in line 37. Keep moving down the line till you find the problem, examine each line carefully. FlippinAwesomeCrew 62 — 6y
0
That seems to be the problem thanks. I just changed lined 47's "aoecheck.Value = aoecheck.Value +1" into "aoentity.Parent.AoeDebounce.Value = aoentity.Parent.AoeDebounce.Value +1". But I still don't understand why can't i use my first statement insead. I thought I've already declared that "aoecheck = aoentity.Parent:FindFirstChild("AoeDebounce")" don't they mean the same thing? lesliesoon 86 — 6y
0
Also, with my first statement why does it work only after i hit myself the second time the aoe? lesliesoon 86 — 6y
0
I'm sorry I don't know FlippinAwesomeCrew 62 — 6y
Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

I finally got "aoecheck" to work

--declaring shortcuts
fireball = script.Parent
remote = fireball.RemoteEvent
player = fireball.Parent.Parent
character = player.Character
mouse = player:GetMouse()
dmgdebounce = false
aoedebounce = false
testvalue = 0

remote.OnServerEvent:connect(function()

    local projectile = fireball.Handle:Clone()        
    local bv = Instance.new("BodyVelocity",projectile)
    bv.MaxForce = Vector3.new(math.huge,math.huge,math.huge)          
    bv.Velocity = mouse.hit.lookVector * 50           
    projectile.Parent = game.Workspace                    

    projectile.Touched:connect(function(entity)

        local humanoid = entity.Parent:FindFirstChild("Humanoid")
        if not entity:IsDescendantOf(character) and entity.CanCollide == true then

            --FireBall Damage--
            if humanoid then                  
                humanoid:TakeDamage(10)
                print("took fireball damage")
            end
            --creating an aoe
            local aoe = game.Lighting.AOE:Clone()
            aoe.Parent = workspace
            aoe.CFrame = projectile.CFrame                            
            projectile:Destroy()                          
        --AOE Damage--
            for i = 0,10 do                               
                aoe.Touched:connect(function(aoentity)
                    local aoecounter = game.Lighting.AoeDebounce:Clone()                          
                    local aoehumanoid = aoentity.Parent:FindFirstChild("Humanoid")
                    local aoecheck = aoentity.Parent:FindFirstChild("AoeDebounce")

                    if not aoecheck then  
                        aoecounter.Parent = aoentity.Parent
                    end
                    if aoehumanoid and aoedebounce ==false and aoehumanoid.Health>0 and aoecheck then -- Amendment here 
                        aoedebounce = true
                        aoecheck.Value = aoecheck.Value +1  
                        aoehumanoid:TakeDamage(10)
                        print("took aoe dmg")                 
                    end               
                end)                          
                aoe.Size = Vector3.new(i*5,i*5,i*5)       
                wait(0.05)
            end
            aoedebounce = false
            aoe:Destroy()
        end
    end)
end)

apparently I need to confirm the existence of "AoeDebounce" inside of the target character (making a wait() doesnt work)? Which is why all it requires is an additional "aoecheck" in the if statement (see line 44). I know this isn't a very good explanation but it would be great if someone could confirm with me why is that so. For now i will just take it that if I want to alter or print any variable's object (which in this case is my variable is "aoecheck" and my object is "AoeDebounce" inside of the target character) , I better check its existence inside the if statement.

Answer this question