Part look at mouse

I want a part to follow the player’s mouse. Though my script is not working. I get an error message stating:
Workspace.Part1.Script:3: attempt to index nil with ‘GetMouse’
I dont get it…

local part1 = game.Workspace.Part1 -- The part that will turn to face Part2
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

part1.BodyGyro.cframe = CFrame.new(part1.Position, mouse.Position)
3 Likes

You can’t get the local player in server scripts. You can do this with a local script placed outside of the workspace, preferably in StarterPlayerScripts.

1 Like

Alright fixed that, I should have known better. though it still does not work.

1 Like

Set the part’s CFrame to CFrame.new(Mouse.Hit.Position)

2 Likes

Adding on to @TigerLeo77 you can use RemoteEvents to talk between the client and server

Local script in StarterCharacterScripts

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local event = game.ReplicatedStorage:WaitForChild('MovePartEvent') -- defines the event

mouse.Move:Connect(function() -- fires everytime the mouse moves
  event:FireServer(mouse.Hit) -- send the info to the server
end)

Script in ServerScriptService

local part = -- define part

local event = Instance.new('RemoteEvent') -- Creates the remote for the client to fire
event.Name = 'MovePartEvent'
event.Parent = game.ReplicatedStorage

event.OnServerEvent:Connect(function(cframe) -- Server recieves the mouse.hit through an event
  part.CFrame = cframe
end)
2 Likes

Doesnt work still. Did I edit it correctly?

local part1 = game.Workspace.Part1
local player = game.Players.LocalPlayer
local Mouse = player:GetMouse()

part1.CFrame.new(Mouse.Hit.Position)
1 Like

Is there any other way I can do without using remote events?

1 Like

You’re not setting the created CFrame to anything. Try using this instead:

local tweenService = game:GetService("TweenService")
local player = game.Players.LocalPlayer
local part = workspace.Part
local mouse = player:GetMouse()

mouse.Move:Connect(function()
	local movePart = tweenService:Create(part, TweenInfo.new(0.1), {Position = mouse.Hit.p})
	movePart:Play()
end)

Basically it just tweens the part to the mouse position every time the mouse moves. It’s really wacky though but improving it is on you.

1 Like

If you want to make it client only keep it like that. The last line should be part1.CFrame=CFrame.new(Mouse.Hit.Position)
and it should be in a render stepped loop

local RS = game:GetService("RunService")

RS.RenderStepped:Connect(function(delta)
    --stuff
end
1 Like

Still wont work:

local part1 = game.Workspace.Part1
local player = game.Players.LocalPlayer
local Mouse = player:GetMouse()


local RS = game:GetService("RunService")

RS.RenderStepped:Connect(function(delta)
	part1.CFrame=CFrame.new(Mouse.Hit.Position)
end
1 Like

Also I made it go to the mouse, keep your original line

1 Like

Not if you want the block displayed to all players in the server. If you only care about the block being shown to the current player, you don’t need to use events.

1 Like

@TigerLeo77 @MyUsernames_This @dandcx

Ok this is getting confusing. Everyone is giving me a bunch of different scripts, what is the best way to do it. I want to be able to use it for like a turret on a tank for example. What would the best way for that.

At the start of your post you said you want “a part to follow the player’s mouse” but then your title says “Part look at mouse”… do you want the part to look at the mouse whilst moving or do you want the part just to look at the mouse?

ya uh I got the script off the roblox dev hub and eddited it. Forgot to take out the note.

I’m not sure what you mean there, what exactly are you trying to achieve?

Im going to clerify even more. I should have added this in the descripton. Im making a vehicle and i want it to have a turret on top, and the person can control it with their mouse while in the vehicle seat. i already can make the vehicle drive and now I just need this part.

1 Like
--LocalScript inside StarterPlayerScripts maybe
local part1 = game.Workspace.Part1 -- The part that will turn to face Part2
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local RunService = game:GetService("RunService")

RunService.RenderStepped:Connect(function()
    part1.BodyGyro.CFrame = CFrame.new(part1.Position, mouse.Hit.Position) --CFrame isn't capitalized here, Lua is case-sensitive
end)

Try this?


local part1 = game.Workspace.Part1 -- The part that will turn to face Part2
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

mouse.Move:Connect(function()

part1.CFrame = CFrame.lookAt(part1.Position, mouse.Hit.p)

end)

I mean, you can use the Move event of the mouse so whenever you move your turret it will make sure to face your mouse.

Also, don’t use CFrame.new() if you want to use the second parameter as that is deprecated, use CFrame.lookAt instead.

Should it be a normal script or local? Because it doesnt work…