Making something continuously happen if a key is held down

I am trying to make it so that if a key is held down, every 0.05 seconds it would do something. I tried putting a UIS.InputBegan function into a while loop but for some reason this didn’t work. Does anyone know how I can achieve this?

Can you show the UIS code you wrote?

Sure! here:

local UIS = game:GetService("UserInputService")

while true do
	
	UIS.InputBegan:Connect(function(input)

		if input.KeyCode == Enum.KeyCode.W then

			local Plr = game.Players.LocalPlayer
			local Character = Plr.Character

			if Character.FlightSystemValues.Seated.Value == true and Character.FlightSystemValues.MinimumEngineStarted.Value == true then

				local UpDown = "Up"

				local PlaneName = game.Players.LocalPlayer.Character.FlightSystemValues.PlaneNonLiveryName.Value

				game.ReplicatedStorage.FlightSystem.Throttle:FireServer(Plr, Character, UpDown, PlaneName)

			end

--		elseif input.KeyCode == Enum.KeyCode.S then



		end

	end)
	
	wait(0.05)
	
end

Did some changes. First removed the while loop because it was connecting a new UIS connection every 0.05 seconds which you only need 1 connection, not multiple. Then added a timeout directly into the UIS connection so it checks if you clicked the W key 0.5 seconds ago and if you have it fires

local UserInputService = game:GetService("UserInputService")

local timeout = 0.5
local startTime = os.clock()

UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.W and startTime - os.clock() >= timeout then
		startTime = os.clock()
		print("fired")
	end
end)

Do it the other way around

local UIS = game:GetService("UserInputService")

local Plr = game.Players.LocalPlayer
local Character = Plr.Character or Plr.CharacterAdded:Wait()

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.W then
		while UIS:IsKeyDown(Enum.KeyCode.W) do
			if Character.FlightSystemValues.Seated.Value == true and Character.FlightSystemValues.MinimumEngineStarted.Value == true then
				local UpDown = "Up"
				local PlaneName = game.Players.LocalPlayer.Character.FlightSystemValues.PlaneNonLiveryName.Value
				game.ReplicatedStorage.FlightSystem.Throttle:FireServer(Plr, Character, UpDown, PlaneName)
			end
			task.wait(.05)
		end
	end
end)