Help with mouse input checking

Hi!

I’m currently making a magnum revolver for my game. Right now I haven’t begun scripting but in the brainstorming I was trying to figure out how I would check if the player has held the mouse for a certain amount of time.

Example of what i want to achieve:

(TIME STAMPED)
cred: Adjo on youtube

What i want to achieve:

  • Check if player holds mouse for x amount of seconds, then shoot
  • Instead of resetting timer when let go, count it back down

If anybody has an idea of how i could achieve this, please let me know!

Thank you very much

EDIT: EXAMPLE

5 Likes

I think you can start a timer from when they click with something like

while t < 5 do
wait(1)
t = t + 1
end

Of course I need to see more of what you’re doing but that seems like the basic gist of it.

3 Likes

Okay, when i start to script the function I’ll let you know.
Thanks for the reply!

edit:

how would this also count it back up when you let go?

2 Likes

After it ends set the value back to 0. I can’t be super specific since I don’t see exactly what you’re doing, sadly, but just write

t = 0

after the end somewhere in the script.

3 Likes

the idea was that i would let it ease back afterward, not flatout zero. I’ll start scripting and if im having trouble i’ll let you know.

3 Likes

Oh, you can do easing back like

wait(0.25)
t = 4
wait(0.25)
t = 3
wait(0.25)
t = 2
wait(0.25)
t = 1
wait(0.25)
t = 0

That’s probably a very rough fix, I haven’t seen if there’s a way you can make it properly gradual but that should still ease it back a little.

2 Likes

Wouldn’t ticks() be more effective?

you can just do this

local mouse = plr:GetMouse()

local timeHolding = 0


local function M1Down()
	timeHolding = tick()
end

local function M1Up()
	local realTime = (tick() - timeHolding)
	
	print(realTime)

	timeHolding = 0
end

mouse.Button1Down:Connect(M1Down)
mouse.Button1Up:Connect(M1Up)

this is basically the same thing as what @Innatoth suggested but unlike his suggestion were not doing a while loop each time the player is holding down his mouse.

also I know this is already solved just wanted to post this :stuck_out_tongue:

2 Likes

i actually hadnt tried it yet. Thanks for the reply, again, would i just count it back down when the mouse is up?

3 Likes

I don’t know exactly what you mean, but in my code all I’m doing is setting TimeHolding back to 0.

As far as I know it’s impossible to make a tick count backwards, so if that’s what your trying to do then @Innatoth’s code would be the better and only option.

their might some weird math calculation you can do, but I’m not that smart :man_shrugging:

3 Likes

ah okay, how i wanted it is that if you let go before the timer hits x then the hammer would go back and the timer would to like tween back or whatever

edit:
maybe i could increase a number value and then tween it back when the mouse goes up?

2 Likes

If you have a set value for the hammer like 1 or 2 then tween to it while holding down then when not holding down tween it to zero?

or of course you could use lerp but that would also require while loops so tween is probably best.

5 Likes

yup thought so, thanks very much for the help! i appreciate it lots.

4 Likes

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