What I am trying to do is have a part highlighted with a border when a player hovers over it. I have managed to this with server scripts, however this won’t work as the border will appear for every other player in the server.
Is it possible to use ClickDetectors from a local script? As in, not have to define all of them individually and be able to detect when a player is interacting with one and defining it through that?
You can’t access a ClickDetector from a LocalScript. Instead, I suggest using a RemoteEvent.
You could iterate through all the parts in a location in a server script, check for a ClickDetector, and then assign a function to the ClickDetector’s MouseClick event:
local RS: ReplicatedStorage = game:GetService("ReplicatedStorage")
local click_event: RemoteEvent = RS:WaitForChild("ClickEvent")
local obj: Folder = game.Workspace.ClickableParts
for k, v in pairs(obj:GetChildren()) do
local cd: ClickDetector = v:FindFirstChildOfClass("ClickDetector")
-- IF NO CLICKDETECTOR, SKIP THE CURRENTLY ITERATED OBJECT
if not cd then continue end
-- ON CLICK FIRE REMOTE EVENT TO PLAYER WHO CLICKED
cd.MouseClick:Connect(function(plyr: Player)
click_event:FireClient(plyr)
end)
end
… then, in a LocalScript, hook up the event to do something:
local RS: ReplicatedStorage = game:GetService("ReplicatedStorage")
local click_event: RemoteEvent = RS:WaitForChild("ClickEvent")
function _connect()
--some code
end
click_event.OnClientEvent:Connect(_connect)
yes you can use ClickDetectors on a localscript
this would help with checking if You are the one who clicked but in your case, you should do what @skipper987 suggested