I am trying to make a simple mouse click event in a local script but the script is not detecting the mouse clicks or hover events, the script does run as it prints Script Ran. There are no errors in the console.
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local target = Mouse.Target
local part = target
local CursorId = “2287179355”
– Create a ClickDetector
local ClickDetector = Instance.new(“ClickDetector”)
ClickDetector.Parent = part
ClickDetector.MaxActivationDistance = 1000
ClickDetector.CursorIcon = “rbxassetid://”…CursorId
print(“Script Ran”)
– left mouse click
ClickDetector.MouseClick:Connect(function()
print(“Left Click”)
end)
– right mouse click
ClickDetector.RightMouseClick:Connect(function()
print(“Right Click”)
end)
I assume you’re trying to change the icon when you hover over a BasePart with this script. Your problem here is that this script runs once and you’re creating a clickdetector to detect mouse inputs.
Don’t do that.
Instead, use UserInputService for click detections and :GetPropertyChangedSignal() for detecting Target property changes to change your mouse icon.
Edit: For some reason, :GetPropertyChangedSignal() doesn’t work on Target property so instead you should connect it to RunService.RenderStepped event. Also here is a code sample:
local LocalPlayer = game:GetService("Players").LocalPlayer
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local Mouse = LocalPlayer:GetMouse()
local CustomCursorId = "rbxassetid://2287179355"
local NormalCursorId = Mouse.Icon
RunService.RenderStepped:Connect(function()
if Mouse.Target ~= nil then
Mouse.Icon = CustomCursorId
else
Mouse.Icon = NormalCursorId
end
end)
UserInputService.InputBegan:Connect(function(Input,IsGameProcessed)
if IsGameProcessed == false then
if Input.UserInputType == Enum.UserInputType.MouseButton1 then
print("MouseButton1")
elseif Input.UserInputType == Enum.UserInputType.MouseButton2 then
print("MouseButton2")
end
end
end)
I’m pretty sure your issue is related to these lines:
local Mouse = Player:GetMouse()
local target = Mouse.Target
local part = target
You’re setting that part to the Mouse’s target right when you join the game (what means a random part would be chosen) and it wouldn’t change the part variable to anything else after that.