I’m planning on making some vehicles, and so i did this little test to learn how to detect input efficiently.
However, my code looks rather viscous, and i want to know how can i get rid of this aspect of it, if possible.
There are 2 scripts that build this contraption in total:
ServerScript ArrowsManager, parented to the contraption’s model
LocalScript Controller, parented to ArrowsManager
ArrowsManager
local Arrows = script.Parent:FindFirstChild("Arrows") or script.Parent:WaitForChild("Arrows")
local Seat = script.Parent:FindFirstChild("Seat") or script.Parent:WaitForChild("Seat")
local ToggleEvent = Seat:FindFirstChild("Toggle") or Seat:WaitForChild("Toggle")
local ControllerScript = script:FindFirstChild("Controller") or script:WaitForChild("Controller")
local InputTable = {
KEYW = Enum.KeyCode.W;
KEYA = Enum.KeyCode.A;
KEYS = Enum.KeyCode.S;
KEYD = Enum.KeyCode.D
}
ToggleEvent.OnServerEvent:Connect(function(Player,Bool,Key)
if Bool then
if Key == InputTable.KEYW then
print("pressed w")
Arrows.Forward.Material = Enum.Material.Neon
elseif Key == InputTable.KEYA then
Arrows.Left.Material = Enum.Material.Neon
elseif Key == InputTable.KEYS then
Arrows.Back.Material = Enum.Material.Neon
elseif Key == InputTable.KEYD then
Arrows.Right.Material = Enum.Material.Neon
end
else
if Key == InputTable.KEYW then
print("unpressed w")
Arrows.Forward.Material = Enum.Material.Plastic
elseif Key == InputTable.KEYA then
Arrows.Left.Material = Enum.Material.Plastic
elseif Key == InputTable.KEYS then
Arrows.Back.Material = Enum.Material.Plastic
elseif Key == InputTable.KEYD then
Arrows.Right.Material = Enum.Material.Plastic
end
end
end)
Seat:GetPropertyChangedSignal("Occupant"):Connect(function()
if Seat.Occupant then
ControllerScript:Clone().Parent = Seat.Occupant.Parent
end
end)
Controller
local UserInputService = game:GetService("UserInputService")
local Character = script.Parent
local Humanoid = Character.Humanoid
local Toggle = Humanoid.SeatPart.Toggle
UserInputService.InputBegan:Connect(function(Key,IsTyping)
if not IsTyping then
Toggle:FireServer(true,Key.KeyCode)
end
end)
UserInputService.InputEnded:Connect(function(Key,IsTyping)
if not IsTyping then
Toggle:FireServer(false,Key.KeyCode)
end
end)
Humanoid.Seated:Connect(function(Bool)
if not Bool then
script:Destroy()
end
end)
I hope its understandable