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

How can i teleport an object with Mouse.hit?

Asked by 6 years ago
Edited 6 years ago

Can someone tell me how i can make it work?, i made this but it doesn't work

What i'm trying to do is if i click an object that object will be teleported to me

local Mouse = game.Players.LocalPlayer:GetMouse()
Mouse.Button1Down:Connect(function()

for i=1, 25 do 
    wait(0)
Mouse.Hit.Parent.Nap.CFrame = game.Players.LocalPlayer.Character.HumanoidRootPart.CFrame*CFrame.new(0,0,-2.8)
end)

1 answer

Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

The Hit property of the mouse is a CFrame value, not what the mouse is hovering over. The Target property is what you're looking for. This property will hold the part the mouse is hovering over, nil if no part is being hovered over, so you need to do checks to see if the target exists first.

local client = game:GetService("Players").LocalPlayer
local Mouse = client:GetMouse()

Mouse.Button1Down:Connect(function()
    local target = Mouse.Target
    for i=1, 25 do 
        wait()
        if target and target.Parent:FindFirstChild("Nap") then
            target.Parent.Nap.CFrame = client.Character:GetPrimaryPartCFrame() * CFrame.new(0,0,-2.8)
        end
    end
end)


Hopefully this answered your question, and if it did, then don't forget to hit that "Accept Answer" button. If you have any other questions, then feel free to leave them down in the comments.
0
Thank You Very Much, I Tried Mouse.Target but without the if target and target.Parent:FindFirstChild("Nap") then gave me trouble cause i had to have my mouse pointing in the object but with your's i dont have to aim it Kallisus 43 — 6y
Ad

Answer this question