--Scripted by DermonDarble


local character = script.Parent.Parent
local humanoid = character:WaitForChild("Humanoid")
local animations = {}

for i, animation in pairs(character:WaitForChild("Animations"):GetChildren()) do
	animations[animation.Name] = humanoid:LoadAnimation(animation)
end

local state = "None"

local function Grounded()
	local ray = Ray.new(character.HumanoidRootPart.Position + Vector3.new(0, -4.3, 0), Vector3.new(0, -1, 0))
	local hit, position = game.Workspace:FindPartOnRay(ray, character)
	if hit and hit.CanCollide then
		return(true)
	else
		return(false)
	end
end

local function StopAll()
	for i, animation in pairs(animations) do
		animation:Stop()
	end
end

local function ChangeState(newState)
	if state ~= newState then
		StopAll()
		state = newState
		if animations[state] then
			animations[state]:Play()
		end
	end
end

while true do
	game:GetService("RunService").Stepped:wait()
	if humanoid.Health > 0 then
		if Grounded() then
			local speed = Vector3.new(character.HumanoidRootPart.Velocity.X, 0, character.HumanoidRootPart.Velocity.Z).magnitude
			if speed > 1 then
				ChangeState("Walk")
			else
				ChangeState("Idle")
			end
		else
			local ySpeed = character.HumanoidRootPart.Velocity.Y
			if ySpeed > 0 then
				ChangeState("Jump")
			else
				ChangeState("Fall")
			end
		end
	else
		ChangeState("Dead")
	end
end
