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

Can someone help me with mouse.Target?

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

✓ Accepted answer

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)

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