Why isn't this working?

I made a states string, but Im having an issue, and Im not sure why. It will never work even if the state is equal to Running, and I’ve printed it out and it says “RUNNING”. So how do I fix this?

if state == "RUNNING" and not state == "IDLE" or state == "JUMPING" then

Could we see the entire script please? With relevant code

--\\ 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 IsOn, JumpLength = nil, .3
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 > 0 then
		state = "RUNNING"
	elseif speed == 0 then
		state = "IDLE"
	end
end)

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



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

			local speedBoost = speedWatch(humanoid.WalkSpeed)

			JumpAnimation:Play()

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


			-- Tween
			local Information = TweenInfo.new(JumpLength, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut,0,true,0)

			TweenService:Create(game.Workspace.CurrentCamera,Information, {FieldOfView = DView + 15}):Play()

			task.wait(JumpLength)
			JumpAnimation:Stop()
			IsOn = nil
		end
	end
end)

--\\ Loops //--

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

If you’ve got an expression like this with an ==, >=, <, etc comparison operator, don’t use not. Use ~=. The reason is because not is evaluated first, so it reads the code like so
if (not state) == "IDLE" then
This of course will never work because not always returns true or false, so it will never compare to IDLE.

But beyond that, state can’t be both IDLE and RUNNING, so this is redundant and can be removed altogether. Only check the jumping and the running.

2 Likes

Did you try

if state ~= "IDLE"  -- first gate
and state == "RUNNING" or state == "JUMPING"  -- if not idle and then
then
1 Like

The issue might be here.
Try to replace that with:

if state ~= "IDLE"  and state == "RUNNING" or state == "JUMPING"  then
1 Like

The ‘not’ Boolean logic operator has a higher priority than the other operations within the expression meaning that it gets evaluated first.

not state == "IDLE"
--If 'state' is a string value it will always be considered truthy (evaluate to true).
not true == "IDLE"
false == "IDLE" --This will never evaluate to true.
if state == "RUNNING" and false or state == "JUMPING" then
--The first part of this expression is made redundant by the 'false'.
1 Like