My script doesn't detect if the character has the ball, and my function will not run

  1. What do you want to achieve?

I want the script to be able to detect when the player has the ball or not.

  1. What is the issue?

The problem is that the script doesnt detect if the player has the ball or not
image

  1. What solutions have you tried so far?

I tried to put in different methods for the script to find the ball in the characters hitbox, but none of the methods I have tried worked.

So the function doesnt run even when I hold MouseButton1, so it seems like hasBall has some issues.

local Player = game.Players.LocalPlayer
local Character = script.Parent
local humanoid = Character.Humanoid

local PlrStats = Player:WaitForChild("PlrStats")

local uis = game:GetService("UserInputService")

local Footwork = (PlrStats.Footwork.Value)

local KickMeter = PlrStats.KickMeter

local debounce = false -- debounce for kickmeter

local hasBall = Character:WaitForChild("HumanoidRootPart"):WaitForChild("GetBallHitbox"):WaitForChild("Football") == true


local kicking = uis.InputBegan:Connect(function(input, gpe)
	if gpe then return end
	if input.UserInputType == Enum.UserInputType.MouseButton1 and hasBall then

		debounce = false

		repeat
			KickMeter.Value += (Footwork) -- The KickMeter max is 12
			wait(0.15)
		until
		KickMeter.Value >= 12 or debounce == true
		if KickMeter.Value > 12 then
			KickMeter.Value = 12
		end
	end 
end)



uis.InputEnded:Connect(function(input, gpe)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then

		debounce = true
		game.ReplicatedStorage.Remotes.ShootBall:FireServer(KickMeter.Value)
		kicking:Disconnect()
		hasBall = false
		KickMeter.Value = 0
	end
end)
local Player = game.Players.LocalPlayer
local Character = script.Parent
local humanoid = Character.Humanoid

local PlrStats = Player:WaitForChild("PlrStats")

local uis = game:GetService("UserInputService")

local Footwork = (PlrStats.Footwork.Value)

local KickMeter = PlrStats.KickMeter

local debounce = false -- debounce for kickmeter

local hasBall = Character.HumanoidRootPart:FindFirstChild("GetBallHitbox"):FindFirstChild("Football")

local kicking = uis.InputBegan:Connect(function(input, gpe)
    if gpe then return end
    if input.UserInputType == Enum.UserInputType.MouseButton1 and hasBall then

        debounce = false

        repeat
            KickMeter.Value += (Footwork) -- The KickMeter max is 12
            wait(0.15)
        until
        KickMeter.Value >= 12 or debounce == true
        if KickMeter.Value > 12 then
            KickMeter.Value = 12
        end
    end 
end)



uis.InputEnded:Connect(function(input, gpe)
    if input.UserInputType == Enum.UserInputType.MouseButton1 then

        debounce = true
        game.ReplicatedStorage.Remotes.ShootBall:FireServer(KickMeter.Value)
        kicking:Disconnect()
        hasBall = false
        KickMeter.Value = 0
    end
end)

Try this, hope you tell me if it’s working !

sadly the function still doesnt run

What’s wrong here, as @hollaquetalBRUH modified in your script, is line 15. The :FindFirstChild() method doesn’t return a boolean so you can’t use it alone to determine if the player has the ball, just that it simply exists. Since :FindFirstChild() returns an instance, you can use a different comparison like this:

local hasBall = false
if player:FindFirstChild("Ball") then
    hasBall = true
end
if hasBall == true then
    -- Stuff
end

Moreover this is something you’ll want to check more often than not, only checking it once will mean the value will always be false unless the player starts with the ball. Make sure you check for possession every time the player fires InputBegan.

I dont quite understand what you want me to do. I would guess you mean I have to put that code you wrote into the function before all the stuff happens?

ok I tried something, and it helped to detect when the player has the ball, but now the kickmeter starts just by clicking the m1 instead of holding, and without the ball. Then the kickmeter doesnt go down either, keeps its peak at 12 or 13.

local Player = game.Players.LocalPlayer
local Character = script.Parent
local humanoid = Character.Humanoid

local PlrStats = Player:WaitForChild("PlrStats")

local uis = game:GetService("UserInputService")

local Footwork = (PlrStats.Footwork.Value)

local KickMeter = PlrStats.KickMeter

local debounce = false -- debounce for kickmeter

local kicking = uis.InputBegan:Connect(function(input, gpe)
	if gpe then return end
	
	while wait(0.1) do
	local hasBall = false
	if Character.HumanoidRootPart:WaitForChild("GetBallHitbox"):FindFirstChild("Football") then
		hasBall = true
		end


		if input.UserInputType == Enum.UserInputType.MouseButton1 and hasBall == true then
			debounce = false

			repeat
				KickMeter.Value += (Footwork) -- The KickMeter max is 12
				wait(0.15)
			until
			KickMeter.Value >= 12 or debounce == true
			if KickMeter.Value > 12 then
				KickMeter.Value = 12
			end
		end
	end
end)



uis.InputEnded:Connect(function(input, gpe)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then

		debounce = true
		game.ReplicatedStorage.Remotes.ShootBall:FireServer(KickMeter.Value)
		kicking:Disconnect()
		KickMeter.Value = 0
	end
end)


I have figured out the problem:
local hasBall = Character:WaitForChild(“HumanoidRootPart”):WaitForChild(“GetBallHitbox”):WaitForChild(“Football”) == true

This is the issue, because the hasBall variable is set first time the script is loaded, and it only checks once, and then it’s value is set.
So the solution is to change it to this:
local hasBall = Character:WaitForChild(“HumanoidRootPart”):WaitForChild(“GetBallHitbox”):WaitForChild(“Football”)

And then this:
local kicking = uis.InputBegan:Connect(function(input, gpe)
if gpe then return end
if input.UserInputType == Enum.UserInputType.MouseButton1 and hasBall.Value == true then

	debounce = false

	repeat
		KickMeter.Value += (Footwork) -- The KickMeter max is 12
		wait(0.15)
	until
	KickMeter.Value >= 12 or debounce == true
	if KickMeter.Value > 12 then
		KickMeter.Value = 12
	end
end 

end)

uis.InputEnded:Connect(function(input, gpe)
if input.UserInputType == Enum.UserInputType.MouseButton1 then

	debounce = true
	game.ReplicatedStorage.Remotes.ShootBall:FireServer(KickMeter.Value)
	kicking:Disconnect()
	hasBall.Value = false
	KickMeter.Value = 0
end

end)