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 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)
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…
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 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.
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