Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Script not detecting clickdetector click! Can anyone help?

I'm trying to make it so when you click a part it makes your camera watch the NPC (custom shape) but it won't detect the click

--[[
Workspace
	Model
		Humanoid
			Script (Move script, Global)
		Eye 1 (MeshPart)
			Torso (Weld)
		Eye 2 (MeshPart)
			Torso (Weld)
		Mouth( MeshPart)
			Torso (Weld)
		RootPart (Part)
			ClickDetector
				LocalScript (Script I'm talking about)
]]

script.Parent.MouseClick:Connect(function()
	print("Active") --Doesn't print
	workspace.CurrentCamera.CameraSubject = script.Parent.Parent.Parent
end)

6 comments

0

What is your script parented to?

0

Click detector

0

If you use that LocaScript inside of Workspace then It can't run. Change the script parent to...StarterGui

0

@bostaffmanbulgaria1 Not that kind of script, it's a local script for a click detector

0

It needs to be a Script not a localscript.

0

script.Parent.MouseClick:Connect(function(playerWhoClicked) the player who clicked is passed implicitly as a parameter

1 answer

✓ Accepted answer

Okay, so here is what I assume is happening here.

You have a ClickDetector that is a descendant of Workspace. Parented to said ClickDetector is a LocalScript that has no actual client associated with it.

Well, the LocalScript is the problem. LocalScripts won't run if they don't have a client associated with them. The exception to this rule is a player's Character located in Workspace. Because the Character is associated with a player (aka a client), any LocalScript that is parented to that Character will run, but any LocalScript parented to an NPC will not.

Server communication with a player's camera is not acceptable because FilteringEnabled will prevent it from happening. In order to alter the player's camera, either a RemoteEvent or RemoteFunction should be used so that the ClickDetector on the server can communicate with the player that clicks on the ClickDetector.

You may have not known this before, but MouseClick passes a player argument when it fires, meaning that the first parameter given will have a userdata value that points to the player:

script.Parent.MouseClick:Connect(function(p) -- Variable p gains a player value; this variable will now refer to the player who clicked on the ClickDetector

Using the FireClient() function of a RemoteEvent or the InvokeClient() function of a RemoteFunction, you can communicate with the player passed by MouseClick:

-- Option 1
event:FireClient(p) -- Fires a signal to p, the player passed by MouseClick
-- Option 2
func:InvokeClient(p) -- Yields for p, meaning that it will wait until the client fires OnClientInvoke

2 comments

0

Nice explanation!

0

Thanks, this does work however it doesn't set the camera but at least now it prints!

This discussion is preserved as it was. New answers, comments, and votes are closed.