Help with detecting mouse hold

Trying to detect the mouse hold, it works but my “CURRENT_POWER” variable won’t increase.

Local:



local Tool = script.Parent

local MouseLoc = Tool:WaitForChild("MouseLoc")

local IS_HOLDING
local MAX_POWER = 100
local CURRENT_POWER = 0

local userInputService = game:GetService("UserInputService")

local Tool = script.Parent

local Mouse = nil

coroutine.wrap(function()
	while IS_HOLDING and CURRENT_POWER < MAX_POWER do
		CURRENT_POWER = CURRENT_POWER + 1
		print("holding")
		wait()
	end
	print(IS_HOLDING and CURRENT_POWER < MAX_POWER)
end)()

coroutine.wrap(function()
	while not IS_HOLDING and CURRENT_POWER > 0 do
		CURRENT_POWER = CURRENT_POWER - 1
		wait()
	end
end)()

coroutine.wrap(function()
	while true do
		print(CURRENT_POWER,IS_HOLDING)
		wait(.2)
	end
end)()

userInputService.InputBegan:Connect(function(input)
	print("Input began")
	print(input.UserInputType)
	if input.UserInputType == Enum.UserInputType.MouseButton2 then
		IS_HOLDING = true
	end
end)

userInputService.InputEnded:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton2 then
		IS_HOLDING = false
	end
end)

function MouseLoc.OnClientInvoke()
	return game:GetService("Players").LocalPlayer:GetMouse().Hit.p, CURRENT_POWER
end

There are no errors, thanks for help!

There are no errors in the output.

Hello, how are you doing?

I’m pretty sure this ins’t the correct way to use print()

I am pretty sure that this is the problem:

The moment it sees that “Is_Holding” is false it will break the while loop and stop running. Just do this:


local Tool = script.Parent

local MouseLoc = Tool:WaitForChild("MouseLoc")

local IS_HOLDING
local MAX_POWER = 100
local CURRENT_POWER = 0

local userInputService = game:GetService("UserInputService")

local Tool = script.Parent

local Mouse = nil

coroutine.wrap(function()
	while wait() do
		if IS_HOLDING == true then
			if CURRENT_POWER < MAX_POWER then
				CURRENT_POWER = CURRENT_POWER + 1
			end
		else	
			if CURRENT_POWER > 0 then
				CURRENT_POWER = CURRENT_POWER -1
			end
		end
	end	
end)()

coroutine.wrap(function()
	while true do
		print(CURRENT_POWER,IS_HOLDING)
		wait(.2)
	end
end)()

userInputService.InputBegan:Connect(function(input)
	print("Input began")
	print(input.UserInputType)
	if input.UserInputType == Enum.UserInputType.MouseButton2 then
		IS_HOLDING = true
	end
end)

userInputService.InputEnded:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton2 then
		IS_HOLDING = false
	end
end)

function MouseLoc.OnClientInvoke()
	return game:GetService("Players").LocalPlayer:GetMouse().Hit.p, CURRENT_POWER
end
2 Likes

Also what he is printing is a correct way to do it.

It just prints both of them on the same line. It is basically the same as doing:

print(CURRENT_POWER)
print(IS_HOLDING)

This will return true or false. IS_HOLDING is a bool value (true or false value), and “CURRENT_POWER < MAX_POWER” is checking if the current power is greater than the max power, so that will also return true or false. So basically if both of those statements are true then it will print “True”, if not then it will print “False”

1 Like

I see, i found it strange since i’ve never seen someone do it like that and i’ve never done it like that as well.
Thanks for clarifying

1 Like

Yeah no problem, it does look very strange until you learn about it. I was confused at first too.

1 Like