Function returning false but if statement still running

Hey!

So I have made a custom StateMachine thingy because (imo) roblox’s is kind of lacking, but I have an issue regarding some if statements. The main problem is that the if statement is still running even when the function needed to return true returns false.

code used:

-- IsGrounded() function used
function isGrounded() : boolean
	-- param setup
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {char}
	raycastParams.FilterType = Enum.RaycastFilterType.Exclude
	-- actual ray
	local ray = RayCaster:Ray(LegHeight + extraDistance, root.Position, Vector3.new(0, -1, 0), raycastParams)

	if ray ~= nil then
		return true
	else
		return false
	end
end

-- the jump function
function Jump()
	StateMachine:SetState("Jumping")
	root:ApplyImpulse(Vector3.new(0, JumpUpwardsVelocity * 100, 0))
	task.wait(0.2)
	StateMachine:SetState("Falling")
end

-- the problem
function changeStateBasedOnMovement()
	if humanoid.MoveDirection.Magnitude > 0 and isGrounded() == true then -- returns false and still runs?????
		StateMachine:SetState("Running")
	elseif humanoid.MoveDirection.Magnitude == 0 and isGrounded() == true then
		StateMachine:SetState("Standing")
	end
end

heres a screenshot showing the state and what the IsGrounded function returns:


“Running false” shouldn’t happen, as it should say “Jumping false”.

I’m still not entirely sure what the issue is, as I’m still learning the more advanced topics in lua, and I’m hoping someone with more experience can figure this out since I’ve been stumped for a while.

Any help is gladly appreciated!

Might I ask, what is a “StateMachine”?

basically, roblox has a default State Machine that uses Enum.HumanoidStateType. It is a default value that can be accessed through scripts with Humanoid:GetState(). My problem is that it isn’t exactly easy to create new states (no custom enums), and roblox’s default StateMachine isn’t good enough for the project I am working on.

I see. And you can make new states via scripting them in / modifying some kind of module in the player? (Sorry if it might seem obvious, I’m pretty new and don’t understand too much about Roblox’s more inner workings.)

1 Like

yep.

(might aswell give you this helpful link for the limit: HumanoidStateType | Documentation - Roblox Creator Hub)

Oh wait, I have seen this before when I tried to make a ragdoll module (which eventually worked)! I just didn’t know you could make your own, lol.

I didn’t know they were called State machines until now however.

1 Like

I literally just realized adding a Y-Velocity limit made the bug go away (if anyone has the same problem this is potentially a fix):

function isGrounded()
	-- param setup
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {char}
	raycastParams.FilterType = Enum.RaycastFilterType.Exclude
	-- actual ray
	local ray = RayCaster:Ray(LegHeight + extraDistance, root.Position, Vector3.new(0, -1, 0), raycastParams)

	if ray ~= nil then -- grounded
		root:SetAttribute("Grounded", true) -- idk why i changed this tbh
	else -- in air
		root:SetAttribute("Grounded", false)
	end
end

function changeStateBasedOnMovement()
	if humanoid.MoveDirection.Magnitude > 0 and root:GetAttribute("Grounded") == true then
		StateMachine:SetState("Running")
	elseif humanoid.MoveDirection.Magnitude == 0 and root:GetAttribute("Grounded") == true then
		StateMachine:SetState("Standing")
	elseif root:GetAttribute("Grounded") == false and StateMachine:GetState() ~= "Falling" and root.AssemblyLinearVelocity.Y > 10 then -- hard limit on y-vel
		StateMachine:SetState("Jumping")
	elseif root:GetAttribute("Grounded") == false and StateMachine:GetState() ~= "Falling" and root.AssemblyLinearVelocity.Y <= 10 then
		StateMachine:SetState("Falling")
	end
end