Loop wont reset values

  1. What do you want to achieve?

I want it so when the player has the football, and if they hold down m1 a meter charges up bit by bit until 12. And when the player shoots, looses the ball or lets go of m1 the meter will reset back to 0.

  1. What is the issue?
    Right now when you click m1 no matter if you have the ball or not, the meter will go up by itself when having the ball equipped. In the clip, after I shoot and get the ball again, it goes up by itself. But I only want it to go up when I hold down m1.

https://gyazo.com/6f8523afb987427bdf0093abc59c3cd8

  1. What solutions have you tried so far?
    I have tried to use a while loop instead, and tried to switch around with the debounce, but nothing has worked.
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

	KickMeter.Value = 0

	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 then
			debounce = false

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

Do you have a separate function where debounce is being set to true? It might be useful to detect when the input (in this case, MB1 being pressed) ends using InputEnded, to ensure that the mouse is actually being held down.

no thats it for the script. But could you help me a little with detecting when it is being held? I have tried so many things and I am lost lol

local kickEnded = uis.InputEnded:Connect(function(input,  gpe)
    if gpe then return end
    
    if input.UserInputType == Enum.UserInputType.MouseButton1 and hasBall then
        debounce = true
        
        -- you could also do other things like setting the KickMeter to 0 from here instead of the other function
    end
end)

Also, debounce should be set to false somewhere else, as it’s completely redundant otherwise.

local kicking = uis.InputEnded:Connect(function(input, gpe)
if gpe then return end

if input.UserInputType == Enum.UserInputType.MouseButton1 then
debounce = true
end
end)

kicking.Disconnect() – You can change this to kicking:Disconnect() if you don’t have things you want to do with kicking later

kicking = uis.InputEnded:Connect(function(input, gpe)
if gpe then return end
if input.UserInputType == Enum.UserInputType.MouseButton1 then
debounce = true
end
end)

A:

You can make use of the TweenService to create a smooth transition between the values of your KickMeter. You can also use a TweenInfo object to make the tween take a specific amount of time:
local tweenInfo = TweenInfo.new(1)
local tween = TweenService:Create(KickMeter, tweenInfo, {Value = 12})
tween:Play()

Now you can use this in a loop to create a meter that will charge with a smooth transition:
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 = false

local kicking = uis.InputBegan:Connect(function(input, gpe)
if gpe then return end

KickMeter.Value = 0

while wait(0.1) do
    if Character.HumanoidRootPart:WaitForChild("GetBallHitbox"):FindFirstChild("Football") then
        hasBall = true
    end


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

        repeat
            local tweenInfo = TweenInfo.new(1)
            local tween = TweenService:Create(KickMeter, tweenInfo, {Value = 12})
            tween:Play()
            wait(1)
        until
        KickMeter.Value >= 12 or debounce == true or not hasBall
        if KickMeter.Value > 12 then
            KickMeter.Value = 12
        end
    end
end

end)

kicking.Disconnect() – You can change this to kicking:Disconnect() if you don’t have things you want to do with kicking later

kicking = uis.InputEnded:Connect(function(input, gpe)
if gpe then return end
if input.UserInputType == Enum.UserInputType.MouseButton1 then
debounce = true
end
end)

1 Like

I completely forgot to send the rest of the script, idk how I didnt see that

uis.InputEnded:Connect(function(input, gpe)
if gpe then return end
if input.UserInputType == Enum.UserInputType.MouseButton1 then
debounce = true
game.ReplicatedStorage.Remotes.ShootBall:FireServer(KickMeter.Value)
KickMeter.Value = 0
end
end)

Well, I tried to use what you sent me, but its still the same problem as before. By just clicking m1 once, the meter will go up to 12 automaticly, and with what you sent me another problem occured, when it has hit 12, it doesnt change the value, no matter if I shoot the ball or looses it.

But I want the meter to ONLY go up, when I hold down m1 while having the football. And if I let go, shoot or lose the ball, the meter will go down to 0. And by not having the ball, the meters value should be at 0 at ALL times.

I hope you know what I have to do, cause I am lost lol

This is the current script:
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 = false

local TweenService = game:GetService(“TweenService”)

local kicking = uis.InputBegan:Connect(function(input, gpe)
if gpe then return end

KickMeter.Value = 0

while wait(0.1) do
if Character.HumanoidRootPart:WaitForChild(“GetBallHitbox”):FindFirstChild(“Football”) then
hasBall = true
end

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

    repeat
        local tweenInfo = TweenInfo.new(1)
        local tween = TweenService:Create(KickMeter, tweenInfo, {Value = 12})
        tween:Play()
        wait(1)
    until
    KickMeter.Value >= 12 or debounce == true or not hasBall
    if KickMeter.Value > 12 then
        KickMeter.Value = 12
    end
end
end

end)

uis.InputEnded:Connect(function(input, gpe)
if gpe then return end
if input.UserInputType == Enum.UserInputType.MouseButton1 then
debounce = true
game.ReplicatedStorage.Remotes.ShootBall:FireServer(KickMeter.Value)
KickMeter.Value = 0
end
end)

yeah, I forgot to send the kick bit, but I sent it in my previous reply