Help with lag while holding key

I’m trying to get my game to not lag when the player holds E which makes the GUI over my ufo play its tween.

The lag only starts when the player holds E and stops shortly after, I have no idea why this would cause lag when it didn’t in a previous version of the script which did not work the way I intended. I can still move my mouse around which is shown in the video provided, and no error messages come from my script.

I’ve looked on the devforum for a solution as well as trying to fix this issue myself, but I have not yet found a fix to the lag. Any help would be appreciated!!

My script:

local playerservice = game:GetService("Players")
local player = playerservice.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local hrp = char:FindFirstChild("HumanoidRootPart")
local ufoName = player.UserId
local ufo = workspace:FindFirstChild(ufoName)
local RunService = game:GetService("RunService")

local maxDist = 15

RunService.Heartbeat:Connect(function()
	if ufo.inRange.Value == false then
		if player:DistanceFromCharacter(ufo.PrimaryPart.Position) <= maxDist then
			ufo.inRange.Value = true
			print("in range")
		end
	elseif ufo.inRange.Value == true then
		if player:DistanceFromCharacter(ufo.PrimaryPart.Position) > maxDist then
			ufo.inRange.Value = false
			print("out of range")
		end
	end
end)

local TS = game:GetService("TweenService")
local TI = TweenInfo.new(2,Enum.EasingStyle.Linear,Enum.EasingDirection.Out,0,true,0)
local bar =  ufo.UFOTOP:WaitForChild("UFOHOOD1").BillboardGui.Frame.Frame
local sizeAnim = TS:Create(bar, TI, {Size = UDim2.new(0.9, 0, 0.9, 0)})

local UIS = game:GetService("UserInputService")
local KeyToHold = "E"
local E_Being_Held
local heldTime
local neededTime
local holding = false
local finished = false
local Time = 2

UIS.InputBegan:Connect(function(info)
	if info.KeyCode == Enum.KeyCode[KeyToHold] then
		if ufo.inRange.Value == true then
			local Distance = player:DistanceFromCharacter(ufo.PrimaryPart.Position)
			if ufo ~= nil and Distance ~= nil then
				if Distance <= maxDist and sizeAnim.PlaybackState == Enum.PlaybackState.Begin or sizeAnim.PlaybackState == Enum.PlaybackState.Completed or sizeAnim.PlaybackState == Enum.PlaybackState.Cancelled then
					print("playing")
					sizeAnim:Play()
					holding = true
					neededTime = math.ceil(tick())
					heldTime = math.ceil(tick())
					while holding do
						heldTime = math.ceil(tick())
						if heldTime - neededTime >= Time then
							print("finished")
							finished = true
							heldTime = nil
							neededTime = nil
							break
						end
					end
				end
			end
		end
	end
end)

UIS.InputEnded:Connect(function(info)
	if info.KeyCode == Enum.KeyCode[KeyToHold] then
		if holding == true and sizeAnim.PlaybackState == Enum.PlaybackState.Playing then
			print("stopped holding while playing")
			sizeAnim:Cancel()
			bar.Size = UDim2.new(0.9, 0, 0, 0)
			holding = false
			heldTime = nil
			neededTime = nil
		elseif holding == true and sizeAnim.PlaybackState == Enum.PlaybackState.Completed then
			print("stopped holding after completed")
			holding = false
			heldTime = nil
			neededTime = nil
		end
	end
end)
1 Like
while holding do
	heldTime = math.ceil(tick())
	if heldTime - neededTime >= Time then
		print("finished")
		finished = true
		heldTime = nil
		neededTime = nil
		break
	end
end

little bro forgot to add task.wait() here

I’m not sure how to add task.wait() into this part of my code, would you be able to provide an example?

while holding do
	heldTime = math.ceil(tick())
	if heldTime - neededTime >= Time then
		print("finished")
		finished = true
		heldTime = nil
		neededTime = nil
		break
	end
    task.wait()
end

this worked wonderfully! thank you!!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.