Why isn't the bool value checking

Hello Developers,
I am coding a football for a league and I am confused on why this code isn’t working. The ball is going to the characters Rootpart. But the Bool Value that suppose to check when its attached to the rootpart isn’t checking please help.

The Code:

-- Variables
local Ball = script.Parent.Parent
local BallValue = script.Parent.Parent.Values:GetChildren()
local Attachment = script.Parent.Parent:WaitForChild( 'Attach' )

-- Main Code
Ball.Touched:Connect(function(PickedUp)
	if not PickedUp.Parent:FindFirstChild( 'Humanoid' ) then return end
	if PickedUp.Parent:FindFirstChild( Ball ) then return end
	
	-- Attach
	Attachment.Part0 = PickedUp.Parent:FindFirstChild( 'HumanoidRootPart' )
	
	-- Check Ball
	BallValue.HasBall.Changed:Connect(function()
		if Attachment.Part0 == PickedUp.Parent:FindFirstChild( 'HumanoidRootPart' ) and BallValue.HasBall.Value == false then
			BallValue.HasBall.Value = true
		else
			BallValue.HasBall.Value = false
		end
	end)
end)

Maybe because you are using FindFirstChild with an instance in the third line of your main code?

1 Like

I think the problem is that you added GetChildren() in the BallValue variable. Remove GetChildren() from the variable, see if that’ll work.
It should look like this:

local BallValue = script.Parent.Parent.Values -- removed GetChildren()

You can also use WaitForChild or FindFirstChild method when mentioning the Boolean Value, though this may not be needed.

BallValue.HasBall.Changed:Connect(function()
   if Attachment.Part0 == PickedUp.Parent:FindFirstChild("HumanoidRootPart") and BallValue.HasBall.Value == false then
      BallValue:FindFirstChild("HasBall").Value = true
   else
      BallValue:FindFirstChild("HasBall").Value = false
   end
end)
2 Likes

Sadly the script didn’t work. Thank you for helping though

1 Like

Sadly the code didn’t work. Thanks for attempting to help though

1 Like

Is this a script or Local Script?

Okay so the script is located inside of a folder called Code and inside the folder the scripts name is PickupServer cause its a serverscript. The bool value is in its sperate folder called Values. That were I store all the values so its more organized when I want to add a new value or a script

All the folders are inside the football

Hey @anxlr, this is Pumpy.

You actually gotta change the value outside of the Changed event, otherwise the code inside the Changed event will never actually run.

The Code;

-- Attach
Attachment.Part0 = PickedUp.Parent:FindFirstChild('HumanoidRootPart')
BallValue.HasBall.Value = true -- Simple fix
	
-- Check Ball
BallValue.HasBall.Changed:Connect(function()
	if Attachment.Part0 == PickedUp.Parent:FindFirstChild('HumanoidRootPart') and BallValue.HasBall.Value == false then
		BallValue.HasBall.Value = true
	else
		BallValue.HasBall.Value = false
	end
end)