Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How can I have a seat that that rotates on button held?

I'm having a hard time figuring out how to rotate a Seat part on an axis while being seated when I hold a button. For example: When i press "A" I want the part to rotate to the left until I stop pressing "A". How would I best go about doing this in the most simple way?

1 comment

0

Have a loop that rotates the seat whenever a variable is true. Detect when a player presses and releases "A" and turn the variable on and off.

1 answer

You can use UserInputService for this.

To rotate the Seat, you can multiply its CFrame with CFrame.Angles().

-- make sure this is a local script inside StarterGui or StarterPlayerScripts
local UserInputService = game:GetService("UserInputService")

local seat = workspace.Seat -- i assume there's a Seat named "Seat" in workspace
local rotateSpeed = 5 -- you can change this to how fast you want to rotate

local rotateSeat = coroutine.create(function() -- this can be used to pause or play the rotation loop
	while true do
		local seatCFrame = seat:GetPivot() -- similar to seat.CFrame
		local rotateCFrame = CFrame.Angles(0, math.rad(rotateSpeed), 0) -- this will rotate sideways (if speed is negative, clockwise; if speed is positive, counter-clockwise)
		
		seat:PivotTo(seatCFrame * rotateCFrame) -- similar to `seat.CFrame *= rotateCFrame`
		
		task.wait()
	end
end)

local function onPress(input, gameProcessedEvent)
	if not gameProcessedEvent then -- if player is not typing, etc.
		if input.KeyCode == Enum.KeyCode.A then -- if player pressed A
			rotateSpeed = math.abs(rotateSpeed) -- makes the spinner go counter-clockwise (positive)
			coroutine.resume(rotateSeat)
		elseif input.KeyCode == Enum.KeyCode.D then -- if player pressed D
			rotateSpeed = 0 - math.abs(rotateSpeed) -- makes the spinner go clockwise (negative)
			coroutine.resume(rotateSeat)
		end
	end
end

local function pressEnded(input, gameProcessedEvent)
	if not gameProcessedEvent then -- if player is not typing, etc.
		if (input.KeyCode == Enum.KeyCode.A) or (input.KeyCode == Enum.KeyCode.D) then
			coroutine.close(rotateSeat) -- ends the thread
			rotateSeat = coroutine.create(function() -- makes the thread again to replay
				while true do
					local seatCFrame = seat:GetPivot() -- similar to seat.CFrame
					local rotateCFrame = CFrame.Angles(0, math.rad(rotateSpeed), 0) -- this will rotate sideways (if speed is negative, clockwise; if speed is positive, counter-clockwise)
					
					seat:PivotTo(seatCFrame * rotateCFrame) -- similar to `seat.CFrame *= rotateCFrame`
					
					task.wait()
				end
			end)
		end
	end
end

UserInputService.InputBegan:Connect(onPress) -- when a player press a button
UserInputService.InputChanged:Connect(onPress)
UserInputService.InputEnded:Connect(pressEnded)

To create mobile buttons for mobile support, use ContextActionService.

This discussion is preserved as it was. New answers, comments, and votes are closed.