Problem with Skydiving Animation

I am trying to make a skydiving system .I made an animation for this and a parachute.
What Im trying to do is "When the character jumps , it checks if there is a part under the character. If not , then the animation will play. I am trying to do that .I have tried raycasting.But there is no change.Also I’m new to scripting .So here is the script.If there is a problem, inform me.

local UIS = game:GetService("UserInputService")
local player = game:GetService("Players").LocalPlayer
local char = workspace:FindFirstChild(player.Name) or workspace:WaitForChild(player.Name)
local hrp = char:WaitForChild("HumanoidRootPart")
local hum = char:WaitForChild("Humanoid")

local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://animationid"
local anim = hum:LoadAnimation(animation)

local freefalling = false
local canOpenChute = false

local rayOrigin = hrp.Position
local rayDirection = hrp.Position + Vector3.new(0, -10, 0)
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {char}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

function openChute()
	    local chute = game.ReplicatedStorage.Chute:Clone()
	    chute.Parent = char
		local motor6D = Instance.new("Motor6D",chute.Handle)
		motor6D.Part0 = hrp
		motor6D.Part1 = chute.Handle
end
function freefall()
	local currentState = hum:GetState()
	if currentState == Enum.HumanoidStateType.Freefall then
		local raycastResult = workspace:Raycast(rayOrigin,rayDirection, raycastParams)
		if raycastResult then
			local hitPart = raycastResult.Instance
			if not hitPart then
				local bodyVelocity = Instance.new("BodyVelocity", hrp)
				bodyVelocity.Name = "Freefall"
				bodyVelocity.MaxForce = Vector3.new(0, 100000, 0)
				bodyVelocity.P = 1000000
				bodyVelocity.Velocity = Vector3.new(0, -200, 0)
				bodyVelocity.Parent = hrp
				print("Successfull")
				anim:Play()
				if not freefalling then
					freefalling = true
				end
				print(freefalling)
			end
		end
	end
end

hum.StateChanged:Connect(function(oldState, newState)
	if newState == Enum.HumanoidStateType.Freefall then
			freefall()
		UIS.InputBegan:Connect(function(input, gameProcessedEvent)
			if gameProcessedEvent then return end
			if input.KeyCode == Enum.KeyCode.Space then
				if freefalling then
					if not canOpenChute then
						canOpenChute = true
						if canOpenChute then
							openChute()
						end
					end
				end
			end
		end)
	
	elseif newState ~= Enum.HumanoidStateType.Freefall then
		local check = hrp:FindFirstChild("Freefall")
		if  check   and freefalling then
			freefalling = false
			print(freefalling)
			anim:Stop()
			check:Destroy()
			local function checkingGround()
				if not freefalling and hum.FloorMaterial ~= nil then
					local chute = char:FindFirstChild("Chute")
					if chute then
						chute:Destroy()
					end
				end
			end
			checkingGround()
		end
	end
end)
1 Like