I have a certain spotlight model that should follow the cursor, but it doesn’t seem to follow the cursor when its a model.
local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()
local Head = game.Workspace.Spotlight1.Arm.Head
Mouse.Move:connect(function()
local MousePosition = Mouse.Hit.p
Head.CFrame = CFrame.new(Head.Position,MousePosition)
end)
Any way to fix this?
(Extra question: Is there a way to make it so the part only follows the mouse on the x or y axis?)
I have a previous script that allows the spotlight to move using WASD keys.
local userInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local SpotlightHead = game.Workspace.Spotlight1.Arm
local UpdateCon
userInputService.InputBegan:Connect(function(input, gameProcessedEvent)
if input.UserInputType ~= Enum.UserInputType.Keyboard then return end
if input.KeyCode ~= Enum.KeyCode.A then return end
if UpdateCon then
UpdateCon:Disconnect()
UpdateCon = nil
end
UpdateCon = RunService.Heartbeat:Connect(function(dt)
local newCFrame = SpotlightHead.PrimaryPart.CFrame * CFrame.Angles(0, -0.05, 0)
SpotlightHead:SetPrimaryPartCFrame(newCFrame)
end)
end)
userInputService.InputEnded:Connect(function(input, gameProcessedEvent)
if input.UserInputType ~= Enum.UserInputType.Keyboard then return end
if input.KeyCode ~= Enum.KeyCode.A then return end
if not UpdateCon then return end
UpdateCon:Disconnect()
UpdateCon = nil
end)
It moves the entire object using the A key.
Is there any way to change this into looking at the cursor?
You can edit the pivot point of the model using a button at the top of studio (not sure exactly where) and then rotate it 180°. To make it only rotate in x and y you can do this:
local direction = Vector3.new(mouseposition.X,Model.WorldPivot.Position.Y,mouseposition.Z)
Model:PivotTo(CFrame.new(Model.WorldPivot.Position, direction))
Mouse.Move:Connect(function()
local MousePosition = Mouse.Hit.Position
local direction = Vector3.new(MousePosition.Z,Head.WorldPivot.Position.Y,MousePosition.Y)
Head:PivotTo(CFrame.new(Head.WorldPivot.Position, direction))
end)
You may need to make a new Model to hold the Spotlight and add a motor to the Arm and move it that way. I have not used motors in a while, but this should work. local Player = game:GetService(“Players”).LocalPlayer
local Mouse = Player:GetMouse()
local Spotlight = Instance.new(“Model”, game.Workspace)
local Arm = Instance.new(“Part”, Spotlight)
Arm.Name = “Arm”
local Head = Instance.new(“Part”, Spotlight)
Head.Name = “Head”
local ArmMotor = Instance.new(“Motor6D”, Arm)
ArmMotor.Part0 = Arm
ArmMotor.Part1 = Head