Hi there,
for a game I am working on there is a mechanic that involves clicking on an asset, followed by an action. To trigger this script, I am using Mouse.Target. This works kinda, sometimes it does and sometimes it doesn’t. The script will return nil when it doesn’t while I am very clearly clicking on one of the assets (print(mouse.Target.Name) prints nil). Obviously this has to be fixed for a game to work properly, but what should I do instead? Should I use raycasting with userinputservice? Perhaps a clickdetector? Or is there any way to optimize the functionality of Mouse.Target? Please leave your recommendations down below, thanks for reading.
Here is the script
--// Variables
wait(1)
local pickaxe = script.Parent
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
[...]
local db = false
--// Script
local function activated()
print("Tool activated")
if db == false then
db = true
if cooldown.Value == false then
cooldownEvent:FireServer(plr)
[...]
if mouse.Target then
print(mouse.Target.Name)
if mouse.Target.Name == "Hitbox" then
local hitbox = mouse.Target
[...]
end
end
end
db = false
end
print("End Swinghandler")
end
pickaxe.Activated:Connect(activated)```