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)
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.
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)
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.
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
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
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.
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?
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.
--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)
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.