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

How to use keyboard keys to control constraints?

Okay, so what I have right now is a hovercraft that uses "thrusters" that can swivel to provide movement going forwards/backwards and in turning. Here is the current script I use for controlling these constraints, which is placed inside a VehicleSeat.

local frontleft = script.Parent.Parent.FL.FL -- Front Left Thruster
local backleft = script.Parent.Parent.BL.FL -- Back left thruster, etc. etc.
local backright = script.Parent.Parent.BR.FL
local frontright = script.Parent.Parent.FR.FL

while true do -- Steering and Fowards/Back Controls
	wait()
	if script.Parent.Throttle == 1 then
		frontleft.move.TargetAngle = 45
		backleft.move.TargetAngle = 45
		backright.move.TargetAngle = -45
		frontright.move.TargetAngle = -45
	end
	if script.Parent.Throttle == 0 then
		frontleft.move.TargetAngle = 0
		backleft.move.TargetAngle = 0
		backright.move.TargetAngle = 0
		frontright.move.TargetAngle = 0
	end
	if script.Parent.Throttle == -1 then
		frontleft.move.TargetAngle = -45
		backleft.move.TargetAngle = -45
		backright.move.TargetAngle = 45
		frontright.move.TargetAngle = 45
	end
	if script.Parent.Steer == 1 then
		frontleft.steer.TargetAngle = -20
		backleft.steer.TargetAngle = 20
		backright.steer.TargetAngle = -20
		frontright.steer.TargetAngle = 20
	end
	if script.Parent.Steer == 0 then
		frontleft.steer.TargetAngle = 0
		backleft.steer.TargetAngle = 0
		backright.steer.TargetAngle = 0
		frontright.steer.TargetAngle = 0
	end
	if script.Parent.Steer == -1 then
		frontleft.steer.TargetAngle = 20
		backleft.steer.TargetAngle = -20
		backright.steer.TargetAngle = 20
		frontright.steer.TargetAngle = -20
	end
end

What I want to do is use the Q and E keys to make the thrusters face to the side and provide sideways thrust (basically strafing). I tried looking at the Wiki article for ContextActionService, but I don't know how to apply it to what I am doing. I am still a somewhat noob to scripting, but any help is appreciated. Thanks in advance, -legolegolego456

1 answer

UserInputService

-- note that this service only works in LocalScripts
game:service("UserInputService").InputBegan:connect(function(key,gp)
	if gp == false then -- if the players isn't typing
		if key.KeyCode == Enum.KeyCode.Q then
			-- strafe left
		elseif key.KeyCode == Enum.KeyCode.E then
			-- strafe right
		end
	end	
end)

Search InputObject and UserInputService on the wiki for some context and explanation of this event.


ContextActionService

-- note that this service only works in LocalScripts
function strafe_left()
	-- code
end

function strafe_right()
	-- code
end

local ca = game:service("ContextActionService")
ca:BindAction("StrafeLeft",strafe_left,false,Enum.KeyCode.Q) -- action name (unique), function to bind to, create touch button, key (available formats: string, KeyCode, UserInputType, PlayerActions)
ca:BindAction("StrafeRight",strafe_right,false,Enum.KeyCode.E)

Once again, if you need explanation, search ContextActionService, as well as the 'available formats' on the wiki.


I don't have time to provide more info, sorry!

Hope I helped!

~TDP

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