How would I make a crouch mechanic similar to the one from minecraft?

What I mean by the title is I want to make it so that if you press / hold crouch you will not be able to walk of an edge.

I have tried raycasting:

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		character.Humanoid.Running:Connect(function()
			local RayParams = RaycastParams.new()
			RayParams.FilterType = Enum.RaycastFilterType.Blacklist
			RayParams.FilterDescendantsInstances = {character}
			
			local RayResult = workspace:Raycast(character.HumanoidRootPart.Position, Vector3.new(0,-10, 0), RayParams)
			if RayResult then
				print(RayResult.Instance:GetFullName())
				--Check if the player is by the edge or not
			end
		end)
	end)
end)

But I don’t know how I would check if they are on the edge.

If your player is standing on blocks you could create invisible walls when the key is down, then when the key is released you can delete the walls.

For checking if they are on the edge just get the block under the player and check for adjacent blocks and create the walls according to that.

User Input Service (If you want to include consoles)

Mouse(PC Only)