In what way can I make it so that when I press Q, the part keeps moving in a direction instead of only moving once when I press Q?
local userInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Part = game.Workspace.Part
userInputService.InputBegan:Connect(function(input, gameProcessedEvent)
if input.UserInputType == Enum.UserInputType.Keyboard then
if input.KeyCode == Enum.KeyCode.Q then
print("You pressed Q")
Part.Position += Vector3.new(0,0,0.1)
end
end
end)
userInputService.InputEnded:Connect(function(input, gameProcessedEvent)
if input.UserInputType == Enum.UserInputType.Keyboard then
if input.KeyCode == Enum.KeyCode.Q then
print("You released Q")
end
end
end)
Once you press the key, you can set up a temporary Heartbeat connection that will update the position every frame. Once the key is released, you can disconnect the connection:
local RunService = game:GetService("RunService")
local STUDS_PER_SECOND = 10
local UpdateCon
userInputService.InputBegan:Connect(function(input, gameProcessedEvent)
if input.UserInputType ~= Enum.UserInputType.Keyboard then return end
if input.KeyCode ~= Enum.KeyCode.Q then return end
if UpdateCon then
UpdateCon:Disconnect()
UpdateCon = nil
end
UpdateCon = RunService.Heartbeat:Connect(function(dt)
Part.Position += Vector3.new(0, 0, dt * STUDS_PER_SECOND)
end)
end)
userInputService.InputEnded:Connect(function(input, gameProcessedEvent)
if input.UserInputType ~= Enum.UserInputType.Keyboard then return end
if input.KeyCode ~= Enum.KeyCode.Q then return end
if not UpdateCon then return end
UpdateCon:Disconnect()
UpdateCon = nil
end)
Figuring this script out was just the first part of what I was doing.
For the object I’m actually using, is there any way to make it so the object rotates instead of moving in a certain direction? (Just say what specifically I need to change in order to do that)
Instead of changing the position using a new vector you would change the parts cframe position and multiplying it by is Euler rotation: its CFrame.fromEulerAnglesXYZ(0,0,0).
But @Rare_tendo already solved your initial question.
The rotation works on the normal part I was using.
I am trying to implement this script into a model though, and it keeps saying, CFrame is not a valid member of Model “Workspace.Spotlight.Head”
Basically, it’s a spotlight that I’m trying to control with keybinds, but I am trying to go inside the spotlight, and the head is the movable part.
[EDIT]
Here is the script currently
local userInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local SpotlightHead = game.Workspace.Spotlight.Head
local UpdateCon
userInputService.InputBegan:Connect(function(input, gameProcessedEvent)
if input.UserInputType ~= Enum.UserInputType.Keyboard then return end
if input.KeyCode ~= Enum.KeyCode.Q then return end
if UpdateCon then
UpdateCon:Disconnect()
UpdateCon = nil
end
UpdateCon = RunService.Heartbeat:Connect(function(dt)
SpotlightHead.CFrame = SpotlightHead.CFrame * CFrame.fromEulerAnglesXYZ(0.1, 0, 0)
end)
end)
userInputService.InputEnded:Connect(function(input, gameProcessedEvent)
if input.UserInputType ~= Enum.UserInputType.Keyboard then return end
if input.KeyCode ~= Enum.KeyCode.Q then return end
if not UpdateCon then return end
UpdateCon:Disconnect()
UpdateCon = nil
end)