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

How do I make a block follow the player's mouse?

Asked by 7 years ago

I have remotely no idea how to do this. I tried

foundBlock.Position = Vector3.new(mouse.Hit.p)

but it didn't work.

1 answer

Log in to vote
8
Answered by 7 years ago

You're actually very close.

First thing you have to do, is make sure you're using a Local Script.

-- Local Script

Now you have to make sure the Local Script is placed somewhere where it can run. I'll be using the StarterPack.

-- Local Script In StarterPack
You could also you StarterGui, and other places as well. It really doesn't matter as long as the local script isn't in Workspace or ServerScriptService.

Now get the player and the mouse,

-- Local Script In StarterPack
local plr = game.Players.LocalPlayer
local Mouse = plr:GetMouse()

Let's also define our brick at we want to follow the mouse,

-- Local Script In StarterPack
local plr = game.Players.LocalPlayer
local Mouse = plr:GetMouse()
local Brick = workspace:WaitForChild("Brick")

I'll be using a mouse move event instead of a loop because it's more efficient and less choppy.

-- Local Script In StarterPack
local plr = game.Players.LocalPlayer
local Mouse = plr:GetMouse()
local Brick = workspace:WaitForChild("Brick")

Mouse.Move:connect:function()
    --Do stuff later
end)

Now we can use Mouse.Hit to get the CFrame of the mouse in the 3d world and set it to the brick's CFrame.

-- Local Script In StarterPack
local plr = game.Players.LocalPlayer
local Mouse = plr:GetMouse()
local Brick = workspace:WaitForChild("Brick")

Mouse.Move:connect:function()
    Brick.CFrame = Mouse.Hit
end)

I hope that helps you!

Good Luck!

If I did help you, don't forget to accept my answer!
Ad

Answer this question