Event for mouse being held down

Hey there!

I am currently creating a script that generates a part beneath you, like the paths in obbys. When you reach the end there are usually path givers.

As of now, it only generates when you click. I want it so it will generate when you hold down the LMB and stop generating when you release LMB.

Is there like an event that does this?

(Script: If you need it)

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()
local GeneratePathEvent = ReplicatedStorage:WaitForChild("GeneratePathEvent")

local Character = Player.Character or Player.CharacterAdded:Wait()
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")

Mouse.Button1Down:Connect(function()
	GeneratePathEvent:FireServer(HumanoidRootPart.Position - Vector3.new(0, 2, 0))
end)

Start the path with UserInputService InputBegan and end the with InputEnded there’s also an IsMouseButtonPressed function but you will want to use the latter.

Is UserInputService better than the Mouse?

Yes, according to the API reference UserInputService and ContextActionService should be used over Mouse

image

1 Like

You can use UserInputService or ContextActionService, i’d probably use ContextActionService for this case/

UIS
UserInputService.InputBegan:Connect(function(input)
   if input.UserInputType == Enum.UserInputType.MouseButton1 then
        drawPath() -- draw the path
   end
end)

UserInputService.InputEnded:Connect(function(input)
   if input.UserInputType == Enum.UserInputType.MouseButton1 then
       stopDrawingPath()
   end
end)

CAS
local function DrawPath(actioName,inputState,inputObject)
     if inputState == Enum.UserInputState.InputBegan then
         -- start drawing
     elseif inputState == Enum.UserInputState.InputEnded then
        -- stop drawing
     end
end

ContextActionService:BindAction(draw,DrawPath,false,Enum.UserInputType.MouseButton1)
1 Like

Why would you use CAS? (Ch limit)

1 Like

CAS is recommended if you need an action done at a certain point, because you can easily Bind and Unbind actions. The example they use is a car horn, when the player enters a car you bind the “Honk” action and when they leave you unbind it, versus using UIS and then having to check if they are in the car or not.

Here read this Article on it

Also, I used your method. It still doesn’t generate on hold, only on click.

you have to shape your code around it. you would have when InputBegan, every time the player moves their Mouse you would place a path or something like that, you have to build your code logic into it. Then on InputEnded stop that logic from running (or just use CAS)

I still have honestly no idea what to do.

Can you paste your “path generating” code so I can walk you through it

Oh yeah, um sorry I forgot about that. Here:

local Players = game:GetService("Players")
local ContextActionService = game:GetService("ContextActionService")

local Player = Players.LocalPlayer

local Character = Player.Character or Player.CharacterAdded:Wait()
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")

script.Parent.Equipped:Connect(function()
	local function GeneratePath(ActionName, InputState, InputObject)
		if InputState == Enum.UserInputState.Begin then
			local Path = script.YellowPath:Clone()
			Path.Parent = workspace:WaitForChild("Paths")
			Path.Position = HumanoidRootPart.Position - Vector3.new(0, 2, 0)
			Path.Anchored = true
			Path.CanCollide = true
			Path.Transparency = 0.5
			wait(5)
			for i = Path.Transparency, 1, Path.Transparency/4 do
				Path.Transparency = i
				wait()
			end
			if Path.Transparency == 1 then
				Path:Destroy()
			end
		end
	end
	ContextActionService:BindAction("GeneratePath", GeneratePath, false, Enum.UserInputType.MouseButton1)
end)

script.Parent.Unequipped:Connect(function()
	ContextActionService:UnbindAction("GeneratePath")
end)

ok i’ve reviewed and I can’t tell what it’s supposed to do so I’ll just post the code I would do

local function GeneratePath(ActionName, InputState, InputObject)
	if InputState == Enum.UserInputState.Begin then
         -- connect something that generates path when the player moves or whatever you want
    elseif InputState == Enum.UserInputState.End then
         -- disconnect
	end
end

script.Parent.Equipped:Connect(function()
	ContextActionService:BindAction("GeneratePath", GeneratePath, false, Enum.UserInputType.MouseButton1)
end)

script.Parent.Unequipped:Connect(function()
	ContextActionService:UnbindAction("GeneratePath")
end)

Maybe there’s a better way.

I don’t know exactly what you are doing you can also look at the code in free path generator models.


but here are tutorials:
UserInputService
ContextActionService