Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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)

1 comment

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.

4 answers

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

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!

5 comments

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

0

relevant in my case*

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.

0

thank you

0

No problem. Please accept my answer.

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

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.

14 comments

0

@crueluu does local mouse = game.Players.LocalPlayer:GetMouse() apply to your solution?

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?

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.

0

ok

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)

0

rep*

0

wasnt* (sry about my spelling errors)

0

Ok Then, So Like Will You Send Me The Script Itself I Might Be Able To Help.

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

0

i put the code as an answer to my question, reply has character max on it

0

Can You Edit Your Original Comment? And Place The Script There?

0

i dont see an edit button

0

its down the page theres a"Edit" "Remove" "Report"

0

i edited it

This discussion is preserved as it was. New answers, comments, and votes are closed.