ClickDetector wont fire clicked events

I’m working on a game and I have a click detector that i’m using on a model. However, recently, the click detector is not firing any of it’s events, and I have no idea why, as it was working before.

The cursor is still changing to the default click cursor when I hover over it. I added a print statement to the clicked event, and the print statement isn’t even run. I have no idea why this is happening.

Here is a code snippet

script.Parent:WaitForChild('Select').MouseClick:Connect(function(plr)
	print('event received')
	if plr.Team == script.Parent.Data.Team.Value then
		print('selected')
		selected.Value = true
		game.ReplicatedStorage.AddToSelected:FireClient(plr, script.Parent, true)
		script.Parent.SelectionBox.Visible = true
	end
end)

Neither prints are run. I also added breakpoints, and the line that connects the function IS being run, but the function itself is never run.
Why is this happening?

You can add prints statements for the plr.Team and the script.Parent.Data.Team.Value right before the conditional, so you can know if they are equal.

I assume you know how to do it, but here’s how I mean to put it. Just incase:

print('event received')

print(plr.Team)
print(script.Parent.Data.Team.Value)

if plr.Team == script.Parent.Data.Team.Value then

The problem here is, the ‘event received’ print isn’t even being printed, at all.

Oh? What instance is “Select”?

“Select” is a click detector that is a part of the model.
Here is a view of the hierarchy.
screenshothierarchyrts
Everything inside the “unitstuff” folder is cloned to each unit by the server, each unit being a model.
This was working earlier, so I’m not sure what’s happening here.
I’ve also verified that all of the items in here are in fact being cloned over, as they are visible in the hierarchy. No scripts are disabled either.

Hmm, that’s strange… Just to make sure, are you using a local script or a server script?

(For the part that’s supposed to be clicked, just not to confuse you.)

The code snippet I sent is a part of the “UnitScript” script.

I would recommend just having the script be a child to the click detector, this would make the script easier because you could have something like this

script.Parent.MouseClick:Connect(function(plr)
	print('event received')
	if plr.Team == script.Parent.Data.Team.Value then
		print('selected')
		selected.Value = true
		game.ReplicatedStorage.AddToSelected:FireClient(plr, script.Parent.Parent, true)
		script.Parent.Parent.SelectionBox.Visible = true
	end
end)
1 Like

This is how I always do my click detectors and it always seams to work flawlessly.

The thing is, I don’t beleive this makes a difference. It would also require restructuring the variables in the code. Plus, the script isn’t just for the click detector, it also handles several other things related to units such as movement.
The script was recognizing and using the clickdetector completely fine and normally before, and I see no reason as to why it wouldn’t work now.

I apologize, I was just uninformed about what else was in this script
As part of a possible solution, have you made any adjustments to this part of the script recently? If so, were they major and could you perhaps revert the place to a time where it was working?

How about the part, are they cloned with local scripts?

It wouldn’t matter, it would only make a change to all clients if it was done through the server

Strangely, putting the selection code into another script fixed the problem entirely. Why, I have no idea.

1 Like

Do you click detectors work as children of folders or models?

Your code might wait into obilivion which is why both print statements wont fire

1 Like

Does that mark this question as solved already, or are you still having a problem? Just to know.

I figured it might be because the UnitScript would run even before getting cloned to the part, you should make it disabled and only enabled when it actually gets cloned into the part you want the click to work on.

Sorry, I had to go for a moment.
The unitstuff folder is a part of ReplicatedStorage, so it doesn’t run until it’s cloned. However, I’ve encountered a new problem. None of the code inside the UnitScript seems to be running, and functions aren’t being connected.
So it seems this is a problem with the UnitScript not connecting functions properly. It is running, as breakpoints are being hit, but the script just wont run.