Table index is nil

I’m to make a stick pvp game, and it is working so far. However, I pull a weird error in the stick script that does not actually break it, I just can’t figure out how to fix it.

The issue is when I use the script the console saus “Table Index Is Nil”"

-- The Script


local debounce = false


script.Parent.Parent.Activated:Connect(function()
	if debounce == false then
		debounce = true
		local anim = script.Parent.Parent.Parent.Humanoid:LoadAnimation(script.Parent.Animation)
		anim:Play()
	wait(1)
		debounce = false
	end
	


end)
local PlayersHit = {}

script.Parent.Touched:Connect(function(Hit)

	if Hit.Parent:FindFirstChild("Humanoid") and Hit.Parent ~= script.Parent.Parent then

		if debounce == true and PlayersHit[Hit.Parent] == nil then

			Hit.Parent:FindFirstChild("Humanoid"):TakeDamage(16)
			debounce = true
			Hit.Parent.Head.BillboardGui.Enabled = true
			Hit.Parent.Head.BillboardGui.TextLabel.Text = "- 16"
			local player = game.Players:GetPlayerFromCharacter(script.Parent.Parent.Parent)
			if Hit.Parent.Humanoid.Health  <= 0  then
		
				player.leaderstats.Kills.Value += 1 * player.leaderstats.Multiplier.Value
			end
			script["Punch Kit Beefy Hit 1 (SFX)"]:Play()
			
			
			
			
			
			
			PlayersHit[Hit.Parent] = true
			wait(1)
			PlayersHit[Hit.Parent] = nil
			Hit.Parent.Head.BillboardGui.Enabled = false

		end
	end
end)

script.Parent.Parent.Equipped:Connect(function()
	local player = script.Parent.Parent.Parent

	player:WaitForChild("Humanoid").WalkSpeed = 22
end)

script.Parent.Parent.Unequipped:Connect(function()
	local player = script.Parent.Parent.Parent.Parent

	player.Character.Humanoid.WalkSpeed = 16
end)

script.Parent.Parent.Destroying:Connect(function()
	local s, e = pcall(function()
		script.Parent.Parent.Parent.Humanoid.WalkSpeed = 16
	end)
	if e then
		
	elseif s then
		
	end
	
	local ss, ee = pcall(function()
		script.Parent.Parent.Parent.Parent.Character.Humanoid.WalkSpeed = 16
		
	end)
	if ee then
		
	elseif ss then
		
	end
end)

This only happens when I kill the dummmies I have set up, which have fully functional humanoids and everything. As for the weird table in the script, I don’t remember exactly what I was doing… but it works?

Does anyone know how to fix this? It would be really helpful! :slight_smile:

1 Like

Genuinely can’t read this. What’s line 43 on the script?

1 Like

The line is

The error is happening because Hit.Parent seems to be nil. Not sure why this would be the case, but there should be a check if exists and if it doesn’t it can skip that line.

Wdym cant read this - this is basic code

Maybe I just use a pcall for it

They meant that they can’t see the line numbers on the devforum, making it hard to locate what line the error was on.

You shouldn’t use a pcall for these kinds of things. That line giving an error because it tries to set index nil in PlayersHit shouldn’t be handled with a pcall, it should be handled with an if statement, because it’s something you can easily account for without the use of a pcall.

It should be:

PlayersHit[Hit.Parent.Name] = true

You cannot index a table with an instance. It must be a string or number.

Yes but what is the condition of the if statement?

Oh! I will try this, thank you!

The condition would be if Hit.Parent then, which would check if Hit.Parent is not nil.

This is false. You can in fact index a table with an instance. Whether to use the instance or the instance’s name to index it is up to preference.