MouseHold Combat System

How would i detect how long someone is holding the MouseButton?, My initial plan is to make animations play on the client and server detect what damage to assigns. any help ???

Is this what you need sorry if it isn’t I have not used animations before.

local Tool = script.Parent

Tool.Activated:Connect(function()
	Animation:Play()
end)

Tool.Deactivated:Connect(function()
	Animation:Stop()
end)
  1. You clearly didn’t read what im asking. im asking how would i detect how long the mouse is being held for. Nowhere did i say i was using tools. Im not using tools im using MouseButton Click.

What i need a reference to is how would i detect how long the mouse is being held for. And how would i achieve it sever-client sided.

Detect when mouse is down, save current time to a variable called “clickStart” (which is initially stored as a random number, to avoid weird errors), when mouse is up, the time held is just currentTime - clickStart, don’t forget to turn it to seconds if it was in milliseconds or vice versa, depending on your case.

1 Like

How would i check this on the server?, Would i use ContextActionService.

you cant access the mouse on the server. So gather the data on the client and send it in a remote event

1 Like

People can exploit it that way…

Then add a different security check not related to the mouse

I need to find a way to access the tick() time in the server, to assign the damage. if i use a remote event to send information. It can be easily exploited. From my idea, The Client should play Animation. and the Server will assign the Damage Hitbox.

1 Like

How about if you send an event to the server when the player starts holding the mouse, and then another one when they let go of the mouse. So on the first event the server starts a timer on its end and stops the timer when it receives the let go event

1 Like

You’ll want to use UserInputService for this one. The script I’ve provided detects when the left mouse button is pressed and places a checkpoint in time (startTime), then when you release the mouse, the script calculates the elapsed time by subtracting the start time from the current time (endTime).

local userInputService = game:GetService('UserInputService')
local startTime = 0
local isMouseDown = false

userInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if not gameProcessedEvent and input.UserInputType == Enum.UserInputType.MouseButton1 then
		isMouseDown = true
		startTime = tick()
	end
end)

userInputService.InputEnded:Connect(function(input)
	if isMouseDown and input.UserInputType == Enum.UserInputType.MouseButton1 then
		isMouseDown = false
		local endTime = tick()
		local elapsedTime = endTime - startTime
		print('Mouse button held for:', elapsedTime, 'seconds')
	end
end)

I’ve made an edit to account for the gameProcessedEvent variable, and also included isMouseDown to account for a MouseButton1 release if the user pressed down on the mouse during loading.

1 Like

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