Attempt to index nil with 'WaitForChild'

Hey there, I am having an issue accessing the Debounce boolean I created. I added it into the player, however it doesn’t find the instance.

Creating the debounce:

local Debounce = Instance.new("BoolValue")
Debounce.Parent = Player
Debounce.Name = "Debounce"

Main script:

script.Parent.Touched:Connect(function(touch)
	local Player = game.Players:GetPlayerFromCharacter(touch.Parent)
	local debounce = Player:WaitForChild("Debounce")
		
	if debounce == false then
		debounce = true
		Player.leaderstats.Time.Value = Player.leaderstats.Time.Value + 1
		task.wait(1)
		debounce = false
	end
end)
script.Parent.Touched:Connect(function(touch)
	local Player = game.Players:GetPlayerFromCharacter(touch.Parent)
        if Player == nil then
              return
        end
	local debounce = Player:FindFirstChild("Debounce")
        if not debounce then
                return
        end
		
	if debounce.Value == false then
		debounce.Value = true
		Player.leaderstats.Time.Value = Player.leaderstats.Time.Value + 1
		task.wait(1)
		debounce.Value = false
	end
end)

2 things.
Player has the possibility of being nil or not existing.
Debounce was defined as an instance, and not a boolean value.

script.Parent.Touched:Connect(function(hit)
   --> Checks to see if player exists
   local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
   if not Player then
      return
   end

   --> Checks to see if debounce exists
   local Debounce = Player:FindFirstChild("Debounce")
   if not Debounce then
      return
   end

  --> Player & Debounce exist here!
   if Debounce.Value == false then
     -- Do things
   end
end)

You can ingore this since @MasterStudioWorld has the solution pretty much!

I’m literally half asleep, thank you.

1 Like

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