Attempt to index nil bug

I was watching a tutorial on a combat system and I am having a problem with a script that is the exact same to the script on the tutorial but gives me an error. This is the script.

local MuchachoHitbox = require(game.ServerStorage.modules["MuchachoHitboxV1.1"])
local HitService = require(game.ServerStorage.modules.HitService)
local CombatTable = require(game.ServerStorage.modules.CombatTable)

--print(CombatTable)
--[[game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
	character:SetAttribute("Combo", 1)
end)
end)--]]
local remote = game.ReplicatedStorage:WaitForChild("CombatEvent")
local lastremote = game.ReplicatedStorage:WaitForChild("LastHit")
local MaxCombo = 5

local function SetCombo(CV)
	local combo = CV.Combo
	
	if combo < MaxCombo then
		CV.Combo += 1
	else
		CV.Combo = 1
	end
end

local function ComboReset(CV, oldcombo)
	task.delay(1, function()
		local currentcombo = CV.Combo
		
		if oldcombo == 5 then return end
		
		if currentcombo-1 == oldcombo and not CV.Attacking then
			CV.Combo = 1
		end
	end)
end

remote.OnServerEvent:Connect(function(player)
	local character = player.Character
	local humanoid = character:WaitForChild("Humanoid")
	local rootpart = humanoid.RootPart
	local animator = humanoid.Animator
	
	local Attributes = CombatTable[character]
	
	local combo = Attributes.Combo
	local attacking = Attributes.Attacking
	local stunned = Attributes.Stunned
	local blocking = Attributes.Blocking
	
	
	if attacking or stunned or blocking then return end
	Attributes.Attacking = true
	
	local animations = game.ReplicatedStorage["punch anims"]:GetChildren()
	local animation = animator:LoadAnimation(animations[combo])
	
	local hitbox = MuchachoHitbox.CreateHitbox()
	hitbox.Size = Vector3.new(6,6,6)
	hitbox.Offset = CFrame.new(0,0,-2.5)
	hitbox.CFrame = rootpart
	--hitbox.Visualizer = false
	
	task.delay(.25, function()
		ComboReset(Attributes, combo)
		hitbox:Stop()

		if combo == 5 then

			humanoid.WalkSpeed = 5
			task.wait(1)
		end
		humanoid.WalkSpeed = 16
		Attributes.Attacking = nil
	end)
	
	hitbox.Touched:Connect(function(hit, hum)
		if hum == humanoid then return end
		
		local function fx()
			local effectsFolder = game.ReplicatedStorage.fxfolder
			
			local attachment = Instance.new("Attachment")
			attachment.Parent = hum.RootPart
			
			local particle = effectsFolder.hitfx:Clone()
			particle.Parent = attachment
			particle:Emit(5)
			
			local sound = effectsFolder.hitsfx:Clone()
			sound.Parent = attachment
			sound:Play()
			
			local hitanims = game.ReplicatedStorage["hit anims"]:GetChildren()
			local hitanim
			
			if combo == 1 or combo == 3 then
				hitanim = hum:LoadAnimation(hitanims[1])
			elseif combo == 2 or combo == 4 then
				hitanim = hum:LoadAnimation(hitanims[2])
			elseif combo == MaxCombo then
				hitanim = hum:LoadAnimation(hitanims[3])
			end
			
			hitanim:Play()
			
			game.Debris:AddItem(attachment, .1)
			game.Debris:AddItem(sound, .3)
		end
		
		if combo < MaxCombo then
			HitService.Hit(hum, 5, .1, rootpart.CFrame.LookVector*10, false, fx)
		else
			HitService.Hit(hum, 10, .3, rootpart.CFrame.LookVector*100, true, fx)
		end
		
	end)
	
	humanoid.WalkSpeed = 7
	animation:Play()
	task.wait(.16)
	hitbox:Start()
	
	SetCombo(Attributes)
	
end)

The error is :

4 Likes

What does CombatTable and CombatTable[character] return?
Print both of them.


Also please comment the line that caused the error, instead of me manually counting every line.

2 Likes

Line 43 defines the Attributes as: local Attributes = CombatTable[character]

Line 45 is: local combo = Attributes.Combo

On line 45 the variable Attributes is nil because line 43 is not working.

that’s being returned because Player.Character might’ve returned nil

do you have any idea why 43 isn’t working?

is there any solution? i really dont have any experience in scripting and this is my first real try at making a game

the lines are
43 -

local Attributes = CombatTable[character]

45 -

local combo = Attributes.Combo

here’s what comes out when i print the tables
image
image

So Attribute is nil, and the table doesn’t have the Player’s character in that.
Could you elaborate on why are you trying to do CombatTable[character].

the keys in the combat table are strings. player.Character is an instance. You can get the name of the instance with .Name

change it to CombatTable[character.Name]

i dont know, in the tutorial he didnt explain why he wrote that line and it works perfectly fine for him. the like was originally

Local Attribute = CombatTalbe[player.Name]

but it still showed the nil error

it still didn’t work and the line was originally

Local Attribute = CombatTalbe[player.Name]

it worked perfectly find for the guy in the tutorial.

I figured out the problem, in CombatTable, I put the table to find out the player’s name when i could’ve just put it to find the character. The code was originally, (“t” is table, “p” is player.) -

game.Players.PlayerAdded:Connect(function(p)
	p.CharacterAdded:Connect(function(c)
           t[p.Name] = {
             --attributes n stuff
           }
    end)
end)

the fixed version is

game.Players.PlayerAdded:Connect(function(p)
	p.CharacterAdded:Connect(function(c)
           t[c] = {
             --attributes n stuff
           }
    end)
end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.