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

NEW SCRIPTER! Does anyone know how I could debug index nil?

Asked by 4 years ago
Edited 4 years ago

A lot of the time, when I test out my code an error pops up, saying "attempt to index nil with '(certain property)'. If you know how to fix this, please respond.

Code:

game.ReplicatedStorage.SkillEvents["Holy Judgement Skill Events"]["Sun Pillar"].OnServerEvent:Connect(function(player, mousePos) local HolyJudge = game.ReplicatedStorage["Holy Judgement Magic Skills"]["Sun Pillar Skill"] local SunPillar = HolyJudge["Sun Pillar"]:Clone() local SunPillarExplode = HolyJudge["Sun Pillar Explosion"]:Clone() local character = player.Character local localplayer = script.Parent local HolyJudgeCircle = game.ReplicatedStorage["Holy Judgement Magic Skills"].HolyJudgeMagicCircle2:Clone() local HolyJudgeAura = game.ReplicatedStorage["Holy Judgement Magic Skills"].HolyJudgeAura:Clone()

--local mouse = game.Players.LocalPlayer:GetMouse()
--//          \\--
 --|Sun Pillar|--
--\\          //--
wait(1)
HolyJudgeCircle.Position = character.LeftFoot.Position
HolyJudgeAura.Position = character.HumanoidRootPart.Position
SunPillar.Position = character.HumanoidRootPart.Position
HolyJudgeCircle.Parent = workspace
HolyJudgeAura.Parent = workspace
SunPillar.Parent = workspace

print("Checkpoint1") local velocity = Instance.new("BodyVelocity", SunPillar) velocity.Velocity = CFrame.new(SunPillar.Position, mousePos).lookVector * 1

print("Checkpoint2")

--//                    \\--
 --|Sun Pillar Explosion|--
--\\                    //--
SunPillar.Touched:Connect(function(hit)
   if hit:IsDescendantOf(character) or hit:FindFirstChild("ParticleEmitter") then return end
    SunPillarExplode.Position = SunPillar.Position
    wait(1)
    HolyJudgeCircle:Destroy()
    HolyJudgeAura:Destroy()
    SunPillar:Destroy()
    SunPillarExplode.Parent = workspace

print("Checkpoint3")
if hit.Parent:FindFirstChild("Humanoid") then hit.Parent:FindFirstChild("Humanoid"):TakeDamage(20) wait(0.5) hit.Parent:FindFirstChild("Humanoid"):TakeDamage(20) wait(0.5) hit.Parent:FindFirstChild("Humanoid"):TakeDamage(20) wait(0.5) hit.Parent:FindFirstChild("Humanoid"):TakeDamage(20) wait(0.5) hit.Parent:FindFirstChild("Humanoid"):TakeDamage(20)

    --else 

    --hit.Parent:FindFirstChild("Humanoid"):TakeDamage(-100)

end

print("Checkpoint4")
wait(3) SunPillarExplode:Destroy() print("Checkpoint5") end) end)

0
The error message attempt to index nil means that your attempting to change something in the workspace that doesnt exist try using waitforchild for an object in the workspace except humanoids. JesseSong 3916 — 4y

4 answers

Log in to vote
0
Answered by 4 years ago

So, nil is when the script can't find anything. So, say I do game.Workspace.Part3.Name="Part2" If Part3 doesn't exist, it will return something like: Attempt to index nil

Ad
Log in to vote
0
Answered by 4 years ago

Hello.


The error: Attempt to index nil means that you are trying to index (access properties or children) an instance.


Here's an example of a brick that kills you:

Part.Touched:Connect(function(otherPart)
    local humanoid = otherPart.Parent:FindFirstChildOfClass("Humanoid")

    humanoid:TakeDamage(humanoid.Health)
end)

As you might already know, physically simulated things can touch a part, including other parts.

The error Attempt to index nil with TakeDamage would pop up in this situation.


Now to get to debugging.

You'd check what the error says. It says Attempt to index nil with TakeDamage, so we know that the parent of TakeDamage is nil/non-existent.


Now, this is a simple fix. Just add an if statement checking if the humanoid is nil. If it is, it will print "No humanoid found.".

Part.Touched:Connect(function(otherPart)
    local humanoid = otherPart.Parent:FindFirstChildOfClass("Humanoid")

    if humanoid then
        humanoid:TakeDamage(humanoid.Health)
    else
        print("No humanoid found.")
    end
end)

I hope this simple answer helped you. Cheers!

0
Thankyou @youtubemasterWOW, you have cleared things up for me. In my case, I am trying to crate a local variable [local mouse = game.Players.LocalPlayer:GetMouse()], after i create that variable I'm trying to use it to find the mouse's position, it is one of the most important parts of my script, it cant work properly without it. Therefore you answer to avoid the debug in the output will not be O_Ogosh1 0 — 4y
0
relevant in my case* O_Ogosh1 0 — 4y
0
Oh alright. I fortunately know how to solve it. Just add an if statement checking if the mouse's target isn't nil. It will be nil if you're pointing at the sky or something like that. youtubemasterWOW 2741 — 4y
0
thank you O_Ogosh1 0 — 4y
0
No problem. Please accept my answer. youtubemasterWOW 2741 — 4y
Log in to vote
0
Answered by 4 years ago

game.ReplicatedStorage.SkillEvents["Holy Judgement Skill Events"]["Sun Pillar"].OnServerEvent:Connect(function(player, mousePos) local HolyJudge = game.ReplicatedStorage["Holy Judgement Magic Skills"]["Sun Pillar Skill"] local SunPillar = HolyJudge["Sun Pillar"]:Clone() local SunPillarExplode = HolyJudge["Sun Pillar Explosion"]:Clone() local character = player.Character local localplayer = script.Parent local HolyJudgeCircle = game.ReplicatedStorage["Holy Judgement Magic Skills"].HolyJudgeMagicCircle2 local HolyJudgeAura = game.ReplicatedStorage["Holy Judgement Magic Skills"].HolyJudgeAura

local mouse = game.Players.LocalPlayer::GetMouse()
--//          \\--
 --|Sun Pillar|--
--\\          //--
wait(1)
HolyJudgeCircle.Position = character.LeftFoot.Position
HolyJudgeAura.Position = character.HumanoidRootPart.Position
SunPillar.Position = character.HumanoidRootPart.Position
SunPillar.Parent = workspace

@cruelu, it wouldn't let me reply the full thing

Log in to vote
-1
Answered by
crueluu 169
4 years ago
Edited 4 years ago

you might need to wait for the object, by using WaitForChild:(object name)

Can You Send A Code Example, So I Can Look Further Into Your Problem? If You Have One Ofc.

0
@crueluu does local mouse = game.Players.LocalPlayer:GetMouse() apply to your solution? O_Ogosh1 0 — 4y
0
I Dont Know Why But I Feel Like You're Being Passive Aggressive to me Because I Tried To Help, Until I Noticed You DownVoted My Answer.... Really? crueluu 169 — 4y
0
Well If You Want Me To Help Then Send Your Code So I Can See It, Im Not Gonna Know Whats Your Problem is Clearly, Unless Ofcourse You Dont Want Any Help. crueluu 169 — 4y
0
ok O_Ogosh1 0 — 4y
View all comments (10 more)
0
also i didnt downvote you, i just created an account today and it says you need 100 reo to downvote (i want trying to be passive aggressive, you said to send example of my code not the actual thing; that's what i did) O_Ogosh1 0 — 4y
0
rep* O_Ogosh1 0 — 4y
0
wasnt* (sry about my spelling errors) O_Ogosh1 0 — 4y
0
Ok Then, So Like Will You Send Me The Script Itself I Might Be Able To Help. crueluu 169 — 4y
0
game.ReplicatedStorage.SkillEvents["Holy Judgement Skill Events"]["Sun Pillar"].OnServerEvent:Connect(function(player, mousePos) local HolyJudge = game.ReplicatedStorage["Holy Judgement Magic Skills"]["Sun Pillar Skill"] local SunPillar = HolyJudge["Sun Pillar"]:Clone() local SunPillarExplode = HolyJudge["Sun Pillar Explosion"]:Clone() local character = player.Character local loc O_Ogosh1 0 — 4y
0
i put the code as an answer to my question, reply has character max on it O_Ogosh1 0 — 4y
0
Can You Edit Your Original Comment? And Place The Script There? crueluu 169 — 4y
0
i dont see an edit button O_Ogosh1 0 — 4y
0
its down the page theres a"Edit" "Remove" "Report" crueluu 169 — 4y
0
i edited it O_Ogosh1 0 — 4y

Answer this question