Tool Charging with UIS

  1. What do you want to achieve?

I want my tool to charge while I hold it down and then stop when I release the button

  1. What is the issue?

While holding down the button, everything works normally, but when I let go then the power continues and fills up again when I let go

Video:

  1. What solutions have you tried so far?

Roblox Documentation

– My code –

Server:

	if action == "start" then
		print("Starting power charge... Default Power: ", playerPowers[player])

		humanoid.WalkSpeed = 8

		while playerPowers[player] < maxPower do
			if action == "start" then
				playerPowers[player] = playerPowers[player] + powerIncreaseRate
				print("Power: ", playerPowers[player])

				PowerChargeEvent:FireClient(player, "update", playerPowers[player])

				wait(0.1)
			elseif action == "stop" then
				break
			end
		end

		if playerPowers[player] >= maxPower then
			print("Full power")
		end

	elseif action == "stop" then
		humanoid.WalkSpeed = 16
		playerPowers[player] = 0

		PowerChargeEvent:FireClient(player, "stop")

		RockAttackEvent:FireClient(player, playerPowers[player])

		print("Charging stopped")
	end
end)

Client:


UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed then return end
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		PowerChargeEvent:FireServer("start")

		showPowerChargeUI()

		ChargeTrack = humanoid:LoadAnimation(ChargeAnim)
		ChargeTrack.Priority = Enum.AnimationPriority.Action3
		ChargeTrack.Looped = true
		ChargeTrack:Play()
	end
end)

UserInputService.InputEnded:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		PowerChargeEvent:FireServer("stop")

		if ChargeTrack then
			ChargeTrack:Stop()
		end

		hidePowerChargeUI()

		AttackTrack = humanoid:LoadAnimation(AttackAnim)
		AttackTrack.Priority = Enum.AnimationPriority.Action3
		AttackTrack.Looped = false
		AttackTrack:Play()
	end
end)

Hi! I think your remote connecting 2 different functions, here’s solution.
Add variable: local Charging = false
And every while cycle check if variable is true

        Charging = true
		while playerPowers[player] < maxPower and Charging do
			if action == "start" then
				playerPowers[player] = playerPowers[player] + powerIncreaseRate
				print("Power: ", playerPowers[player])

				PowerChargeEvent:FireClient(player, "update", playerPowers[player])

				wait(0.1)
			elseif action == "stop" then
				break
			end
		end

And if action == “stop”

elseif action == "stop" then
        Charging = false
		humanoid.WalkSpeed = 16
		playerPowers[player] = 0

		PowerChargeEvent:FireClient(player, "stop")

		RockAttackEvent:FireClient(player, playerPowers[player])

		print("Charging stopped")
	end
1 Like

It works, I don’t know why I didn’t think of it before, thanks for your help

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