Clicking UIS vs. clicking ClickDetector

I need to get my script working so that when I click a ClickDetector, the UIS click does NOT activate.

As it is currently, what I have tried is changing it from CAS to UIS since I thought maybe GameProcessedEvent would be covered with the clicking a ClickDetector… But it isn’t. I have already looked for solutions and tried that one but to no avail.

If anybody knows a solution to this I’d be very grateful :pray: here is the code for it:

local uis = game:GetService("UserInputService")
local PlayerGui = player.PlayerGui 
local M1 = PlayerGui:WaitForChild("ContextActionGui"):WaitForChild("ContextButtonFrame"):WaitForChild("M1") 

local function M1Click() 
    if player.DisabledCombat.Value == false then 
        combatRemote:FireServer() 
    end 
end 

uis.InputBegan:Connect(function(key,x) 
    if key.UserInputType == Enum.UserInputType.MouseButton1 and not x then 
        M1Click() 
    end 
end) 

M1.MouseButton1Click:Connect(M1Click) 

maybe have a bool variable and check for it when you connect the click UIS event
if they are in more than 1 script you can have that bool as an attribute or a value in a module script

ClickDetectors and UI Buttons all have RBXScriptConnections to make it easier to detect when a player has clicked the clickdetector or UI button.

Click detectors use RightMouseClick and MouseClick to detect when a player has clicked it.

Click Detector Example
Code
local CD = Instance.new('ClickDetector',workspace.SpawnLocation)

CD.MouseClick:Connect(function()
	print('clicked') 
end)

CD.RightMouseClick:Connect(function()
	print('right clicked')
end)
Image

image

Meanwhile, UI buttons (TextButton, ImageButton) use Activated, MouseButton1Click, and MouseButton2Click. There are various other ways of detecting UI clicks, but these ones are much easier to use.

UI Button Example
Code
local UI = game['pathtouielementbutton']

UI.MouseButton1Click:Connect(function()
	warn('mouse1 clicked')
end)

UI.Activated:Connect(function()
	warn('activated')
end)

UI.MouseButton2Click:Connect(function()
	warn('mouse2 click')
end)

All of these can be utilized to detect separate clicks from one another.

Edit: Forgot to put the last code example in a code block format.

it’s in 2 different scripts, and this one is a localscript ofc while the clickdetector is a server script. I am not very familiar with module scripts and idk if that would be a solution, but i do not believe so because the UIS click detection would run before the ClickDetector one would?

The issue is that both of them are activating at the same time, both click detections, but i need only one of them to (the ClickDetector in my case)

You’re using UserInputService to detect when it clicks, that is why. UserInputService detects when input is begun and then runs the code within that connection.

ClickDetector’s only detect when the user clicks the instance itself, not across the entire universe. So you’re code runs whenever the player starts input and that input is the LMB.

I know this already, but how do I fix it-?? I have UIS doing the detection for click for my punching in combat… But I am using a ClickDetector for quest NPCs, so how do I make it so when you click the ClickDetector, it doesn’t run the UIS? (or that it doesn’t run the :FireServer() at least)

you can use attributes
{8CF175B4-11B2-4369-9AD2-27206C3915C7}

-- Only an example
local UserInputService = game:GetService("UserInputService")

local attributeLocation = workspace
local ClickDetector :ClickDetector = workspace

UserInputService.InputBegan:Connect(function()
	if attributeLocation:GetAttribute("AttributeName") == true then
		-- your code
	end
end)

ClickDetector.MouseClick:Connect(function()
	attributeLocation:SetAttribute("AttributeName", false)
end)

make the bool variable false when the click detector is clicked
i gtg rn

local ClickDetector = PATHTOCLICKDETECTOR
local PlayerGui = player.PlayerGui 
local combatRemote = PATHTOREMOTE
local player = PATHTOPLAYER
local M1 = PlayerGui:WaitForChild("ContextActionGui"):WaitForChild("ContextButtonFrame"):WaitForChild("M1") 

local function M1Click() 
    if player.DisabledCombat.Value == false then 
        combatRemote:FireServer() 
    end 
end 

ClickDetector.MouseClick:Connect(function() 
       M1Click() 
end) 

M1.MouseButton1Click:Connect(M1Click) 

*I would also like to point out that combatRemote was never referenced in the script at all; and the player variable was never referenced as well. Without additional context of what and where the script is, I cannot provide help with “player”.

I still think uis would run before the attribute would even get changed but i can try it

i just didn’t put those stuff in the given parts, cause they weren’t really necessary for the task at hand. Just assume it has a reference. I also think your idea of using a remote would also not work because the UIS would run before the ClickDetector could fire the remote back to the client

I took the code from your post and edited it, the remote was already in there.

I have attempted this solution, and I was correct, the UIS runs before the attribute can even be changed. So now what :sweat_smile:

oh, well it doesn’t fix the issue lol it was already referenced in my actual code

I HAVE FOUND A SOLUTION!! ClickDetector comes with .MouseHoverEnter and .MouseHoverLeave connections so I can use that to check when they have their mouse over the NPC and disable their combat accordingly!

1 Like

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