Attempt to index nil with value

I am trying to make a combat script, it was going good until this problem popped up. “Attempt to index nil with value” I made a sepertate script just for the bool values. And the local script is supposed to find it.
Bool Value script(It is inside starter character script)

local PlayerIsAttacking = Instance.new("BoolValue")
PlayerIsAttacking.Parent = game.StarterPack["Basic Combat"]
PlayerIsAttacking.Name = "PlayerIsAttacking"
PlayerIsAttacking.Value = false


local PlayerIsBlocking = Instance.new("BoolValue")
PlayerIsBlocking.Parent = game.StarterPack["Basic Combat"]
PlayerIsBlocking.Name = "PlayerIsBlocking"
PlayerIsBlocking.Value = false


Local script

local combat = script.Parent

local PlayerIsAttacking = combat:WaitForChild("PlayerIsAttacking")
local PlayerIsBlocking = combat:WaitForChild("PlayerIsBlocking")
print(PlayerIsBlocking)

local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game.ReplicatedStorage
local CombatEvent = ReplicatedStorage.CombatEvent
local BlockEvent = ReplicatedStorage.BlockEvent

local LeftPunch = combat:WaitForChild("LeftPunch")
local RightPunch = combat:WaitForChild("RightPunch")
local Block = combat:WaitForChild("Block")

local equipped = false

combat.Equipped:connect(function()
	equipped = true
	print("Equipped!")
end)

combat.Unequipped:connect(function()
	equipped = false
	print("Unequipped!")
end)

combat.Activated:connect(function()
	if equipped and PlayerIsBlocking.Value == false then
		print("Activated")
		PlayerIsAttacking = true
	else
		print("Player is blocking, can't attack!")
	end
end)

3 Likes

This means that you are trying to change the value of something that does not exist

It doesn’t exist? I created the instance inside the bool value script and put it inside the combat tool. WaitForChild doesn’t work either so I don’t know what to do…

Can you screenshot the error so we can see what line its on?

1 Like

It is on line 3 of the local script. It is infinit yield. Sorry I cant take a picture of it

yeah infinite yield basically means what it says, its yielding infinitely because its still waiting for whatever you made it wait for

I dont get it though, I made it so that the bool values are in the tool. And yet, script is still waiting for it???

Go check in inside of the tool while your playtesting and check if its actually in the tool, or maybe your referencing it by the wrong name

When you use waitforchild and it yields then it doesnt know if you spelled whatever your waiting for correctly or not, so that could maybe be the problem

I made sure everything is spelled right, the values were inside the tool, and it still says it

try replacing your server script with this

local char = script.Parent

local PlayerIsAttacking = Instance.new("BoolValue")
PlayerIsAttacking.Parent = char:WaitForChild("Basic Combat")
PlayerIsAttacking.Name = "PlayerIsAttacking"
PlayerIsAttacking.Value = false


local PlayerIsBlocking = Instance.new("BoolValue")
PlayerIsBlocking.Parent = char:WaitForChild("Basic Combat")
PlayerIsBlocking.Name = "PlayerIsBlocking"
PlayerIsBlocking.Value = false

I feel like you should be referencing the player’s backpack if the script is in StarterCharacterScripts.

This would be a new structure for the script in StarterCharacterScripts (assuming it’s a local script)

local Players = game:GetService("Players")

local player = Players.LocalPlayer
    local backpack = player.Backpack

local basicCombat = player.Backpack:WaitForChild("Basic Combat")

-- create the bool values here, parent them to basicCombat

What is stopping you from placing them directly inside the tool in the starter pack? I feel like that would be a more sensible solution.

Also, you wouldn’t have caught this yet, but I found one error in your code so far, it’s on the fifth to last line:

combat.Activated:connect(function()
	if equipped and PlayerIsBlocking.Value == false then
		print("Activated")
		PlayerIsAttacking = true -- HERE
	else
		print("Player is blocking, can't attack!")
	end
end)

It should presumably say PlayerIsAttacking.Value = true.

2 Likes

to fix it put local PlayerIsAttacking = combat:WaitForChild(“PlayerIsAttacking”,99) This makes it so if it doesn’t find the child it will stop in 99 seconds

Thank you all for helping me . I really appreciate this!