Power charging script not working

Hello Devforum! I need help with this issue on my power charging script. I am currently working on a soccer game where you have to charge power to determine the amount of force that will be applied to a ball when kicking it. I created a simple script with userinputservice that detects the holding of a click and then that triggers a while loop that will charge the power over time. The only issue is that the power will keep charging even when the player stops holding the click. I want it so that the amount of power charged will stop when the player stops holding click, therefore ending the loop. I created a value to tell when the click is being held, so that when that value is false, I break the loop. It still did not work though so I am very confused. How would I go about doing this?

Code:

userinputservice.InputBegan:Connect(function(input, gameProcessedEvent)
	if equipped then
		if input.UserInputType == Enum.UserInputType.MouseButton1 then
			holdingclick = true
			local textlabel = player.PlayerGui:FindFirstChild("ShootingPower").TextLabel
			local power = 0
			local maxpower = 50
			print("Holding click")
			while wait() do
				if power < maxpower then
					power = power + 1
					textlabel.Text = "Power: "..power
				elseif holdingclick == false then
					print("Not holding!  Break loop!")
					break
				end
			end
		end	
	end
end)

It’s most likely due to the variable being inside the input function. Every time they click, the value changes back to zero. Move the variable outside of the scope of the input.

local power = 0
local maxpower = 50

You’ll also need to change the way you’re detecting if they are holding down the mouse button.

local buttonDown = false

UIS.InputBegan:Connect(function (input, gpe)
    if input.UserInputType == Enum.UserInputType.MouseButton1 then
        buttonDown = true
    end
end)

UIS.InputEnded:Connect(function (input, gpe)
    if input.UserInputType == Enum.UserInputType.MouseButton1 then
        buttonDown = false
    end
end)

I tried it out and put the variables outside of the event, but it still doesn’t really do what I want it to. The power level still goes to 50 even when the loop is broken.

I edited my response. Please try that.

1 Like

Unfortunately, it is already set like that. This is the full code with the input ended.

local userinputservice = game:GetService("UserInputService")
local powergui = script.Parent:WaitForChild("ShootingPower")
local player = game.Players.LocalPlayer
local equipped = false
local holdingclick = false
local power = 0
local maxpower = 50

script.Parent.Equipped:Connect(function()
	equipped = true
	print("True")
	local guiclone = powergui:Clone()
	print("Cloned")
	guiclone.Parent = player.PlayerGui
end)

userinputservice.InputBegan:Connect(function(input, gameProcessedEvent)
	if equipped then
		if input.UserInputType == Enum.UserInputType.MouseButton1 then
			holdingclick = true
			local textlabel = player.PlayerGui:FindFirstChild("ShootingPower").TextLabel
			print("Holding click")
			while wait() do
				if power < maxpower then
					power = power + 1
					textlabel.Text = "Power: "..power
				elseif holdingclick == false then
					print("Not holding!  Break loop!")
					break
				end
			end
		end	
	end
end)

userinputservice.InputEnded:Connect(function(input, gameProcessedEvent)
	if equipped then
		if input.UserInputType == Enum.UserInputType.MouseButton1 then
			holdingclick = false
			print("Click ended")
		end
	end
end)

script.Parent.Unequipped:Connect(function()
	equipped = false
	local guiclone = player.PlayerGui:FindFirstChild("ShootingPower")
	guiclone:Destroy()
end)

As you can see, the value detecting the click is set right after the input type is met.

Set the power back to 0 before breaking.

EDIT:

I see what you mean, I posted this into my own game. Gimme one second.

Try this, it seems to work in my game.

local userinputservice = game:GetService("UserInputService")
local powergui = script.Parent:WaitForChild("ShootingPower")
local player = game.Players.LocalPlayer
local equipped = false
local holdingclick = false
local power = 0
local maxpower = 50

script.Parent.Equipped:Connect(function()
	equipped = true
	print("True")
	local guiclone = powergui:Clone()
	print("Cloned")
	guiclone.Parent = player.PlayerGui
end)

userinputservice.InputBegan:Connect(function(input, gameProcessedEvent)
	if equipped then
		if input.UserInputType == Enum.UserInputType.MouseButton1 then
			holdingclick = true
			local textlabel = player.PlayerGui:FindFirstChild("ShootingPower").TextLabel
			print("Holding click")
			while holdingclick do
				task.wait()
				print(power)
				if power < maxpower then
					power = power + 1
					textlabel.Text = "Power: "..power
				elseif holdingclick == false then
					print("Not holding!  Break loop!")
					break
				end
			end
		end	
	end
end)

userinputservice.InputEnded:Connect(function(input, gameProcessedEvent)
	if equipped then
		if input.UserInputType == Enum.UserInputType.MouseButton1 then
			holdingclick = false
			power = 0
			print("Click ended")
		end
	end
end)

script.Parent.Unequipped:Connect(function()
	equipped = false
	local guiclone = player.PlayerGui:FindFirstChild("ShootingPower")
	guiclone:Destroy()
end)
1 Like

The problem lies within your if statement inside the loop. The first condition, power < maxpower will always be true regardless of the value of your holdingclick variable. I believe you want your if statement to look like this:

if power < maxpower and holdingclick then
	power = power + 1
	textlabel.Text = "Power: "..power
elseif then
	print("Not holding!  Break loop!")
	break
end
1 Like