Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Can someone help me with mouse.Target?

Asked by 9 years ago

I'm new to this an I wasn't sure how to go about doing this, but here is what I tried doing:

mouse = game.Players.LocalPlayer:GetMouse()

mouse.Changed:connect(function()
    print 'changed'
    if mouse.Target == "GameRock" then
        mouse.Button1Down:connect(function()
            local t = mouse.Target
            t:Destroy()
        end)
    end
end)

there are no errors, either. There is a part that spawns called 'GameRock' and when your mouse is over it, and you click the rock is supposed to destroy, but it doesn't.

1 answer

Log in to vote
2
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
9 years ago

Your problem is that you're trying to compare mouse.Target with a string. But mouse.Target is always going to be either an instance or nil - never a string. So it won't error, but it won't ever work.


Additionally, you're trying to compare it as a condition for the Button1Down event to activate. The event will activate reguardless of whether the condition returns true or false.


Lastly, your Changed event is unnecessary(:


local mouse = game.Players.LocalPlayer:GetMouse()
local keyName = 'GameRock'

mouse.Button1Down:connect(function()
    local t = mouse.Target
    if t and t.Name == keyName then
        t:Destroy()
    end
end)
Ad

Answer this question