Need help with constantly moving part when holding a key

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)

Any help is appreciated! :slightly_smiling_face:

1 Like

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)
3 Likes

This works! Thank you so much!

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.

You can read more about it here:

https://developer.roblox.com/en-us/api-reference/datatype/CFrame

The part just seems to disappear when I tried to apply it.

Here is what I used:

Part.Position = Vector3.new(CFrame.fromEulerAnglesXYZ(0,0,0.1))

It would be more like:

Part.CFrame = Part.CFrame * CFrame.fromEulerAnglesXYZ(math.rad(45), 0, 0)

You can play with the xyz… i just used the math example given in my link.

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)
1 Like

Is the Head a part or a model?

If its a part use cframe, if its model use orientation.

Edit: i just saw that u said its a model, so yes then just adjust its Orientation along the axis or axes you want.

UpdateCon = RunService.Heartbeat:Connect(function(dt)
		SpotlightHead.Orientation = Vector3.new(0, 0.1, 0)
	end)
end)

This doesn’t seem to work, is there an issue somewhere here?

[EDIT]

With this script, it now says "Orientation is not a valid member of Model “Workspace.Spotlight.Head”

Ok this post (read just the solution) shows two ways you can change the orientation of a model and they explain it really well:

2 Likes