Script Check; Asking for Improvements

Hi guys,

I am making post to see if there are any improvements that can be made to my script (which I am sure there are).

Basically it is a Charged Jump script; If the player holds space for more than 1 second then their Jump is twice as strong, if they hold it for less than a second then their jump is normal.

The script functions as it should, but I feel like there are plenty of improvements that can be made to it, but I just don’t know what they are.

Here is the full script:

Summary
local lastCharge 
local chargeAcknowledged = false

UIS.InputBegan:Connect(function(input, gameProcessed)
	if StateTracker.State == "onRunning" then
		if input.KeyCode == Enum.KeyCode.Space then
			humanoid.JumpPower = 0
			if chargeAcknowledged == false then
				if StateTracker.State == "onRunning" then
				chargeAcknowledged = true
				lastCharge = time()
				end
			end	
		
			print(chargeAcknowledged)
			local timeCheck

			while UIS:IsKeyDown(Enum.KeyCode.Space) do
				chargeAcknowledged = false
				timeCheck = time() - lastCharge
				print(timeCheck)
				wait(.1)

				if timeCheck >= 1 then
					humanoid.JumpPower = 100
				elseif timeCheck < 1 then
					humanoid.JumpPower = 50
				end
			end
		
			humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
			
		end
	end
end)

StateTracker.State is what I use instead of HumanoidStateType; it is basically the same thing and in the script I am checking if the Player is on the ground

I added while UIS:IsKeyDown(Enum.KeyCode.Space) do because I was unsure of another way of checking how long Space was being held down for. (this is where I believe the biggest change can be made)

Hello PowerHouse2424!

Some changes that could be made are…

  • Try looking into the ContextActionService, it allows for future expandability with input and eases cross-platform capabilities
  • Having :IsKeyDown() inside of an input began function is not needed, just hook onto .InputEnded. When the input is ended, :Disconnect() the original .InputBegan function.

Currently, your script would ‘overlap’ itself because the function is never disconnected and the :IsKeyDown() would keep running past the first time.

I would consider the way you scripted this a ‘hacky’ solution and I think you could clean it up. Good luck!

1 Like

So I changed the script and just wanted to see if this is what you meant:

local lastCharge 
local chargeAcknowledged = false

UIS.InputBegan:Connect(function(input, gameProcessed)
	if StateTracker.State == "onRunning" then
		if input.KeyCode == Enum.KeyCode.Space then
			humanoid.JumpPower = 0
			if chargeAcknowledged == false then
				chargeAcknowledged = true
				lastCharge = time()
			end	
		
			print(chargeAcknowledged)

		end
	end
end)

local timeCheck

UIS.InputEnded:Connect(function(input, gameProcessed)
	if StateTracker.State == "onRunning" then
		if input.KeyCode == Enum.KeyCode.Space then
			chargeAcknowledged = false
			timeCheck = time() - lastCharge
			print(timeCheck)
			if timeCheck >= 1 then
				humanoid.JumpPower = 100
			elseif timeCheck < 1 then
				humanoid.JumpPower = 50
			end
			
			humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
			wait()
			humanoid.PlatformStand = true
			
		end
	end
end)