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

How do you make stuff happen to a character when mouse is over it?

Asked by
blowup999 659 Moderation Voter
10 years ago

I want functions to happen when i press a key some of the functions include respawning characters. So i need the mouse.Target but that only works on everything but me... I've tried the script below it works on everything but my own character.

plr = game.Players.LocalPlayer
mouse = plr:GetMouse()
mouse.KeyDown:connect(function(key)
if key:lower()=="r"then
mouse.Target:Destroy()
end
end)

1 answer

Log in to vote
0
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
10 years ago

ROBLOX has specifically made it impossible to target any part inside your own character, otherwise you'd get fun errors like launching a rocket at your own face sometimes.

To get around this, you have to code smarter.

I would weld an invisible, uncollideable brick to the character's body, large enough to completely cover them, without creating too many glitches in movement. This brick cannot be parented to the Character in question, so I suggest making it a local brick and then checking to see if it was the brick clicked. If so, kill the player, if not, move on to the rest of the code. You won't be able to just Destroy() the brick, as that won't kill the player.

Also, Destroy()ing the player won't kill them if it destroys the entire model.

Ad

Answer this question