How do I fix my long jump script?

Hello, I am trying to make a long jump script that is basically supposed to emulate the long jump mechanic in Super Mario 64, but its been hard trying to make one that’s pixel perfect.

Basically, Im making a state string, that is supposed to check the current humanoid string and return its value to a text label thats connected to a billboard element on the players head, then change the Text labels text.

But the issue is that, in any state, I can do a long jump, I can spam the long jump, prompting me to get more airtime, and the long jump also doesn’t even apply a correct force when I do spam the long jump. ( LShift + Spacebar ). There isn’t any errors, so I came here to ask for help.

game link

So how can I fix this?

--\\ Variables //--
repeat task.wait() until game.Players.LocalPlayer

local UIS = game:GetService("UserInputService")
local RS = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")

local Character = game.Players.LocalPlayer.Character
local JumpAnimation = Character.Humanoid:LoadAnimation(RS.LongJumpAnimation.LongJumps)
local DView = 70
local Table = {}

local root = script.Parent.PrimaryPart
local humanoid = root.Parent:WaitForChild("Humanoid")
local state = " "

local stateText = script.Parent:WaitForChild("Head"):FindFirstChildOfClass("BillboardGui").TextLabel

--\\ Functions //--

function speedWatch(speed)
	if speed == 24 then
		local boostVal = 1.25
		return boostVal
	elseif speed == 16 then
		local boostVal = 1.15
		return boostVal
	else
		local boostVal = 1
		return boostVal
	end
end

humanoid.Running:Connect(function(speed)
	if speed == 16 then
		state = "WALKING"
	elseif speed == 0 then
		state = "IDLE"
	else
		if speed == 24 then
			state = "RUNNING"
		end
	end
end)

humanoid.Jumping:Connect(function()
	state = "JUMPING"
end)

local Deb = false

UIS.InputBegan:Connect(function(Input, IsTyping)
	if state == "RUNNING" and state ~= "IDLE" or state ~= "JUMPING" or state ~= "LONG_JUMP" then
		if IsTyping then return end
		if UIS:IsKeyDown(Enum.KeyCode.LeftShift) and UIS:IsKeyDown(Enum.KeyCode.Space) and humanoid.Running then
			if not Deb then
				Deb = true
				state = "LONG_JUMP"
				local speed = Character.Humanoid.WalkSpeed

				local speedBoost = speedWatch(humanoid.WalkSpeed)

				JumpAnimation:Play()

				humanoid.Jump = false humanoid.JumpPower = 0
				root:ApplyImpulse(root.CFrame.LookVector * (1000 * speedBoost))
				workspace.Gravity = 145.8
				root:ApplyImpulse(root.CFrame.YVector * (475 * speedBoost))
				task.wait(0.25)
				workspace.Gravity = 196.2
				humanoid.Jump = false humanoid.JumpPower = 50

				JumpAnimation:Stop()
				
				Deb = false
			end
		end
	end
end)

--\\ Loops //--

while task.wait() do
	stateText.Text = "STATE: "..state
	print(state)
end

Probably because of those or statements there, that means even if is idle which is not jumping state, then it becomes true and long jump becomes possible.

One trick I use is to just print out the entire if statement.

print(state == "RUNNING" and state ~= "IDLE" or state ~= "JUMPING" or state ~= "LONG_JUMP")

This will allow you to check if the If statement is acting weird.

But how would I actually know if Im using the and and or functions?

This is the running function

humanoid.Running:Connect(function(speed)
	if speed == 16 then
		state = "WALKING"
	elseif speed == 0 then
		state = "IDLE"
	else
		if speed == 24 then
			state = "RUNNING"
		end
	end
end)

For this function keep in mind speed is a float number, if you print it out sometimes the values are in decimal places like 0.004.

This will fail the if statement as 0 is not equal to 0.0004 check

I recommend checking if the value is within a range instead with > operator or >=

2 Likes