How to create a throttle system?

What do you want to achieve?

I’m working on an aircraft, and I’m trying to make a throttle system using user input service (im using shift and control for the throttle)

What is the issue?

I’ve tried making one currently but it isn’t working.

What solutions have you tried so far?

At the time, I couldn’t find anything on this besides the :IsKeyDown article.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you.

My current script: (local script)

local usp = game:GetService("UserInputService")

local shiftKey = usp:IsKeyDown(Enum.KeyCode.LeftShift)
local controlKey = usp:IsKeyDown(Enum.KeyCode.LeftControl)

local throttle = script.Parent.Parent.Parent:GetAttribute("Throttle")
local currentThrottle = script.Parent.Parent.Parent:GetAttribute("currentThrottle")
	
local function Input()
	
	if not controlKey and shiftKey then
		throttle = 0
	end
	
	if shiftKey then
		throttle = 1
	end
	
	if controlKey then
		throttle = -1
	end
	
end

usp.InputBegan:Connect(Input)

while wait() do
	print(throttle, "Throttle")
end

Thanks,
Luke

2 Likes

You probably don’t want to use a 1 or -1 value to control an aircraft.
Using renderstepped/heartbeat to create a loop and increase the value of the speed is probably a better solution.

And the reason this isn’t working is because ControlKey and ShiftKey are not variables, that doesn’t mean anything.

Input contains a variable that UIS.InputBegan contains, read up on the UserInputService on the roblox documentation.

UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then

end
end)

That should be the proper layout.

I also didn’t see anything about BodyMovers how are you planning on making it fly? That could be a great way to decide how you should control throttle.

1 Like

This is a separate script, as the WASD controls are in a server script, but I’m trying to increase the value as long as the player is holding shift/control

1 Like

How are you detecting user input on the server?

Either way, UserInputService This has a good example at the bottom of the page.

And to increase the speed as you are holding you should use a Renderstepped or Heartbeat to do so.

Also note you won’t need to do a lot of this on the server if you relinquish control to the client using NetworkOwnership.

1 Like

This is in a local script, the control script is a server script. Also, I don’t know much about Renderstepped, how would I use it?

1 Like

Its similar to a while loop, same principle but very different.
https://developer.roblox.com/en-us/api-reference/event/RunService/Heartbeat
https://developer.roblox.com/en-us/api-reference/event/RunService/RenderStepped

Choose the one you prefer, Do read what is different but the Heartbeat runs after physics so it will probably be a better choice.

Then just add to the throttle variable as a total value, that will of course tie into your BodyMovers or what ever you plan on using to make the plane actually fly, that being said How is WASD inputs being picked up on the server script?

1 Like

Throttle, and steer values from a vehicle seat that of which turn hinge constraints. For the turning, theres a torque instance inside the vehicle seat that is manipulated by the current angle of the hinges, it works well. Also, what do you mean a “total value”?

Total value, as in increment the existing value of the throttle as time progresses while holding the throttle+ button.

1 Like

I’m using attributes for this, would I have to use something else? (for the value)

1 Like

Attributes, Int values, are all fine, and all do the same thing.

2 Likes

So, I’m trying what you said but:

local RunService = game:GetService("RunService")

local usp = game:GetService("UserInputService")

local throttle = script.Parent.Parent.Parent:GetAttribute("Throttle")
local currentThrottle = script.Parent.Parent.Parent:GetAttribute("currentThrottle")

throttle = 0

local function throttle(step)
	wait(0.5)
	local function Input(userinput)
		
		if currentThrottle > 100 then
			currentThrottle = 100
		end

		if currentThrottle < 0 then
			currentThrottle = 0
		end	
		
		if userinput.KeyCode == Enum.KeyCode.LeftShift then
			throttle = 1
		end
	
		if userinput.KeyCode == Enum.KeyCode.LeftControl then
			throttle = -1
		end
		currentThrottle = currentThrottle + throttle
	end
	usp.InputBegan:Connect(Input)
end	
RunService.RenderStepped:Connect(throttle)

while wait(0.3) do
	print(currentThrottle, "Throttle")
end

and I get this from my output: (oh and its SUPER laggy)

image

I don’t think you should have a wait(.5) in a function that is being called 30 (or 60) times per second by renderstepped. IDK though. Thats probably the lag you’re experiencing, not sure about the nil error though.

I added this, but I’m not sure if it helps…

image

No, the step will always be ‘every frame’ which will always be LESS than your 2 seconds, you can’t wait longer than the step and not build up lag. (I think). Just get rid of the wait.

I removed the wait, but it still adds lag, I also tried heartbeat, and I’m still using it, but it didn’t seem to help

Okay, well I don’t know then. This shouldn’t even be running unless someone is using the vehicle.
Maybe its the error that’s causing the lag, it has to print out that error message 40+ times per second. Make sure your attribute exists before comparing it to a number in line 14.

It does exist, I created an “If” statement that checks if the throttle even exists, and it prints the “no” message

image

Well, your function doesn’t think it exists. You should test it before you use it. Add print statements to help see whats going on.

I would start by commenting out the renderstep line so the script can get to the print loop and see if it prints a bunch of 0 values or if it prints nil nil nil every .3 seconds.

I just realized, one of my functions is called “throttle” so, I renamed it but its giving me “Attempt to call a nil value” without specification where the problem is. EDIT: alright, I fixed it by updating my :Connect for the Heartbeat()

image

So, I fixed it until I tried to add to the throttle value…

if currentThrottle or throttle ~= nil then
			currentThrottle = currentThrottle + throttle
		else
			print("bruh")
		end

“attempt to preform arithmetic (add) on a nil and number (line 23)”