So, I’m working on an aircraft that uses WASD controls and it uses the steer, and throttle values to turn. I’ve already figured out the throttle system and now I need to incorporate yaw for the rudder.
I want to use the Q and E keys, how could I create a yaw value (in a local script) that changes between; 1, -1, and 0 whenever your holding Q or E. For example: when you hold Q the value is -1, when you hold E the value is 1, but when you let go of the keys, the value is 0.
How would I go about doing this?
Thanks,
– Luke
1 Like
You can use the UserInputService with its InputBegan and InputEnded functions.
With these functions you can do this, for example :
local UserInputService = game:GetService("UserInputService")
local YawValue = 0
UserInputService.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.Q then
YawValue = -1
elseif input.KeyCode == Enum.KeyCode.E then
YawValue = 1
end
end)
UserInputService.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.Q then
YawValue = 0
elseif input.KeyCode == Enum.KeyCode.E then
YawValue = 0
end
end)
Hoping this helps you
1 Like
Hey, I’m getting this error…
Whenever I print the YawValue it sometimes gives me a nil value as well… I think that’s the problem but I don’t know how to fix it
Is it in a localscript ? Or could you send me a screen of the script ?
It’s on a server script, I have to adjust the target angle of a servo, I just changed the “char” value I have to say “no” instead of nil and its printing that message as well even though I only said to print the yaw value…
UserInputService works only on the client side.
However, you can use a Remote Event to send the yaw value when the input begin or end.
I’m using a server script which is where the error is coming from, and I’m already using a remote event for this, the local script is firing the event currently.
So, I advise you to when you fire this event, send a variable which is the yaw value.
I am doing that, but the first variable is always the character so I have to add a char variable for the yaw value to even go through
Okay, I sorta figured it out but the problem is that the input is only detected once I let go…
https://streamable.com/ietvc5
1 Like
You can do something like that :
script.Parent.Event:FireServer("EVENT1",YawValue)
-- Server Side
script.Parent.Event.OnServerEvent:Connect(function(Event1, YawValue)
print(YawValue)
end)
If you use your events for other things, I advise you to use a Table.
Okay, but what about the problem I noted above? Any solution for that?
Do you fire the event when the input starts ?
Here, I don’t know how to check it so here’s my local script:
local char = 0
local currentThrottle = script.Parent.Parent.Parent:GetAttribute("currentThrottle")
local UserInputService = game:GetService("UserInputService")
local event = game.ReplicatedStorage:WaitForChild("YawUpdate")
local YawValue = 0
UserInputService.InputBegan:Connect(function(input)
event:FireServer(YawValue, char)
if input.KeyCode == Enum.KeyCode.Q then
YawValue = -1
elseif input.KeyCode == Enum.KeyCode.E then
YawValue = 1
end
end)
UserInputService.InputEnded:Connect(function(input)
event:FireServer(YawValue, char)
if input.KeyCode == Enum.KeyCode.Q then
YawValue = 0
elseif input.KeyCode == Enum.KeyCode.E then
YawValue = 0
end
end)
You have to fire the event in the if otherwise it fires as soon as a player presses a key
Like that :
UserInputService.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.Q then
YawValue = -1
event:FireServer(YawValue, char)
elseif input.KeyCode == Enum.KeyCode.E then
YawValue = 1
event:FireServer(YawValue, char)
end
end)
Same for input ended
2 Likes
Okay, thank you so much, I really appreciate it!