|
How would I detect if and which part my mouse cursor is pointing at (within a certain distance), and if I press the E button, something happens?
I'm basically trying trying to make an object that you can pick up by pressing the "E" button while looking at it at a certain distance (that way you can't click on it half way across the map).
~MightyDantheman |
|
|
Can anyone at least tell me how to detect if a player is looking at an object? It doesn't have to be the cursor touching it, but I feel like that would be the easiest way to do so.
~MightyDantheman |
|
AethexJoin Date: 2011-10-16 Post Count: 2193 |
If you want to see if the cursor touched it, then you can use the mouse's Moved event along with the mouse's property "Target" to check if the mouse is hovering over the object. |
|
AethexJoin Date: 2011-10-16 Post Count: 2193 |
|
|
|
Thanks, but those are both confusing to me (I'm used to the wiki when it shows you a script example). Is it possible to see an example or something that could explain this to me a little better?
~MightyDantheman |
|
AethexJoin Date: 2011-10-16 Post Count: 2193 |
--localscript
local plr = game.Players.LocalPlayer;
local mouse = plr:GetMouse();
mouse.Move:connect(function()
local tar = mouse.Target;
if tar then
if tar:FindFirstChild("keyPickupPart") then --insert something like a boolvalue name "keyPickupPart" in parts you want to pickup
local uis = game:GetService("UserInputService");
uis.InputBegan:connect(function(inp)
if inp.KeyCode == Enum.KeyCode.E then
-- insert pickup code here
end
end)
end
end
end) |
|
AethexJoin Date: 2011-10-16 Post Count: 2193 |
oh, i didn't implement a distance check
you can use DistanceFromCharacter for that: http://wiki.roblox.com/index.php?title=API:Class/Player/DistanceFromCharacter
let me know if you need me to implement that for you too |
|
|
I can't seem to get this to work. I've tried this script in the local script:
local plr = script.Parent.Parent
local mouse = plr:GetMouse();
mouse.Move:connect(function()
local tar = mouse.Target;
if tar then
if tar:FindFirstChild("keyPickupPart") then --insert something like a boolvalue name "keyPickupPart" in parts you want to pickup
local uis = game:GetService("UserInputService");
uis.InputBegan:connect(function(inp)
if inp.KeyCode == Enum.KeyCode.E then
game.Workspace:ClearAllChildren(
)
end
end)
end
end
end)
--//
and this script for a normal script:
local plr = script.Parent.Parent
local mouse = plr:GetMouse();
mouse.Move:connect(function()
local tar = mouse.Target;
if tar then
if tar:FindFirstChild("keyPickupPart") then --insert something like a boolvalue name "keyPickupPart" in parts you want to pickup
local uis = game:GetService("UserInputService");
uis.InputBegan:connect(function(inp)
if inp.KeyCode == Enum.KeyCode.E then
game.Workspace:ClearAllChildren()
end
end)
end
end
end)
--//
I did put that boolvalue in.
~MightyDantheman |
|
|
AethexJoin Date: 2011-10-16 Post Count: 2193 |
why'd you do "plr = script.Parent.Parent"? My script used "game.Players.LocalPlayer" for the player. It also has to be used in a localscript, that server script you have won't work. |
|
|
I tried it your way inside a local script and it still didn't work. But I got it to work my own way. Thank you for your help.
~MightyDantheman |
|
|
Whats the working script? I could really use it in a game Im making. :) |
|