How can I detect if mouse is held and use it with a remote event

So Im trying to make a script so when the player presses a button it creates a sphere above the player which constantly expands but I want it to be visible to all the players so they also can see the block expanding.
image
The part in the script would be the thing which would be cloned and expanded. The script would wait the cooldown and if the cooldown has already passes then it’d fire the remote event which would fire the script.
The problem is I cant have the script constantly fire the remote event or it’ll give me a bunch of errors and exhaust the time. And I don’t know any other way which everyone else would be able to view the expanding ball besides a remoteEvent. I’ve seen somebody make a script like this before but he never actually showed it and explained how it worked. This is the script so far.

function onButton1Down(mouse)
	if script.Parent.Cooldown.Value == 0 then
		script.Parent.RemoteEvent:FireServer()
		script.Parent.Cooldown.Value = 7
	else
		return
	end
end

function onSelected(mouse)
	mouse.Button1Down:connect(function() onButton1Down(mouse) end)
end

script.Parent.Selected:connect(onSelected)

I’ve checked the developer forms but couldn’t find something which could help me. I’m not sure how I could detect if the mouse is being held and expand the part for everyone not just the local player to see as the player is holding it down.

3 Likes

You can use UserInputService to determine whether a button is currently pressed down. I’ve created a basic example for you:

local UIS = game:GetService('UserInputService')

local mouseDown = false

UIS.InputBegan:Connect(function(key, isSystemReserved)
    if not isSystemReserved then -- isSystemReserved checks if the player is currently in a system-occupied event, such as typing in the chat and in text boxes.
        if key.UserInputType == Enum.UserInputType.MouseButton1 then
            mouseDown = true
            print('Mouse Down')
        end
    end
end)

UIS.InputEnded:Connect(function(key, isSystemReserved)
    if not isSystemReserved then
        if key.UserInputType == Enum.UserInputType.MouseButton1 then
            mouseDown = false
            print('Mouse Up')
        end
    end
end)

For transferring to the server, look into RemoteFunctions and RemoteEvents.

You can also look into UserInputService here:

P.S. On a side note, you should change your :connect() to :Connect() as the former is deprecated.

1 Like

Although that would work I cant transfer it to the server without exhausting the remote event and i’d like it to be in sync with the mouse.

1 Like

Unfortunately that delay is inevitable as UserInputService isn’t replicated across the client/server boundary.